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文件所放位置
在工程下的资源包下,如果需要再放入深入一点的包中,需要在后台代码中将路径改变

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

日霄科技

感谢各位大佬,您鼓励是我的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值