1. 程式人生 > >java後臺解析XML檔案

java後臺解析XML檔案

解析XML檔案分為兩種:
1、DOM(Document Object Model)
2、SAX(Simple API for XML)
DOM是基於XML文件樹結構的解析,SAX是基於事件流的解析。
我用到的是SAX解析>>

一、XML檔案

	      <?xml version="1.0" encoding="UTF-8"?>
	<root>
	        <area position="left">
	                <portal>
	                        <order>left1</order>
	                        <title>推送資訊</title>
	                        <height>0.38</height>
	                        <type>tsxx</type>
	                        <dataurl>http://www.baidu.com</dataurl>
	                </portal>
	                <portal>
	                        <order>left2</order>
	                        <title>最熱關注</title>
	                        <height>0.25</height>
	                        <type>zrgz</type>
	                        <dataurl>http://www.baidu.com</dataurl>
	                </portal>
	                <portal>
	                        <order>left3</order>
	                        <title>點選量排行</title>
	                        <height>0.36</height>
	                        <type>djlph</type>
	                        <dataurl>http://www.baidu.com</dataurl>
	                </portal>
	        </area>
	        <area position="center">
	                <portal>
	                        <order>center1</order>
	                        <title>國家政策</title>
	                        <height>0.33</height>
	                        <type>gjzc</type>
	                        <dataurl>http://www.baidu.com</dataurl>
	                </portal>
	                <portal>
	                        <order>center2</order>
	                        <title>地方政策</title>
	                        <height>0.33</height>
	                        <type>dfzc</type>
	                        <dataurl>http://www.baidu.com</dataurl>
	                </portal>
	                <portal>
	                        <order>center3</order>
	                        <title>電改新聞</title>
	                        <height>0.33</height>
	                        <type>dgxw</type>
	                        <dataurl>http://www.baidu.com</dataurl>
	                </portal>
	        </area>
	        <area position="right">
	                <portal>
	                        <order>right1</order>
	                        <title>會議通知</title>
	                        <height>0.45</height>
	                        <type>hytz</type>
	                        <dataurl>http://www.baidu.com</dataurl>
	                </portal>
	                <portal>
	                        <order>right2</order>
	                        <title>最新發布</title>
	                        <height>0.3</height>
	                        <type>zxfb</type>
	                        <dataurl>http://www.baidu.com</dataurl>
	                </portal>
	                <portal>
	                        <order>right3</order>
	                        <title>我的收藏</title>
	                        <height>0.24</height>
	                        <type>wdsc</type>
	                        <dataurl>http://www.baidu.com</dataurl>
	                </portal>
	        </area>
	</root>

二、後臺解析過程

   SAXReader reader = new SAXReader();
            InputStream in = null;
            try {
                    map.clear();
                    // 通過reader物件的read方法載入zhcxdoc.xml檔案,獲取docuemnt物件。
                    in = TfZhcxdocServiceImpl.class.getResourceAsStream("/zhcxdoc.xml");
                    Document document = reader.read(in);
                    // 通過document物件獲取根節點root
                    Element root = document.getRootElement();
                    // 通過element物件的elementIterator方法獲取迭代器
                    Iterator<Element> rootIt = root.elementIterator();
                    // 遍歷迭代器,獲取根節點中的資訊
                    String key;
                    List<Map<String, String>> list;
                    Map<String, String> mapdata;
                    while (rootIt.hasNext()) {
                            Element area = rootIt.next();
                            key = area.attribute("position").getValue(); // 取xml中某元素的屬性的值
                            Iterator<Element> areaIt = area.elementIterator();
                            list = new ArrayList<Map<String, String>>();
                            while (areaIt.hasNext()) {
                                    Element panel = areaIt.next();
                                    Iterator<Element> panelIt = panel.elementIterator();
                                    mapdata = new HashMap<String, String>();
                                    while (panelIt.hasNext()) {
                                            Element data = panelIt.next();
                                            mapdata.put(data.getName(), data.getStringValue());
                                            map.put(key, list);
                                    }
                                    list.add(mapdata);
                            }
                    }
            } catch (DocumentException e) {
                    e.printStackTrace();
            } finally {
                    try {
                            if (null != in) {
                                    in.close();
                            }
                    } catch (IOException e) {
                            e.printStackTrace();
                    }
            }

三、需要的包

   import java.io.IOException;
	import java.io.InputStream;
	import java.util.ArrayList;
	import java.util.HashMap;
	import java.util.Iterator;
	import java.util.List;
	import java.util.Map;
	import org.dom4j.Document;
	import org.dom4j.DocumentException;
	import org.dom4j.Element;
	import org.dom4j.io.SAXReader;

四、使用該方法XML檔案所放位置
在工程下的資源包下,如果需要再放入深入一點的包中,需要在後臺程式碼中將路徑改變