dom4j使用的小例子

 

product.xml:

<?xml version="1.0" encoding="UTF-8"?> 
<root>
    <product>
        <index id="1">交换机</index>
        <index id="2">传送网</index>
        <index id="3">WLAN</index>
        <index id="4">路由器</index>
    </product>
    
    <scene>
        <index id="1">规划</index>
        <index id="2">实施</index>
        <index id="3">维护</index>
    </scene>
</root>

代码:

package com.cy.test;

import java.io.File;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class ReadXmlFile {
    public static void main(String[] args) throws Exception{
        String path = URLDecoder.decode(ReadXmlFile.class.getClassLoader().getResource("").getPath(), "UTF-8") + "product.xml";
        
        SAXReader reader = new SAXReader();
        Document  document = reader.read(new File(path)); 
        
        //获取文档的根节点
        Element root = document.getRootElement(); 
        
        //获取product的节点
        Element element = root.element("product");
        List<Element> proList = element.elements();
        for(Element e: proList){
            String value = e.getTextTrim();
            Attribute attr = e.attribute("id");
            String key = attr.getValue();
            System.out.println("key:" + key + "--value:" +value);
        }
        
        //获取scene节点
        Element sElement = root.element("scene");
        List<Element> sList = sElement.elements();
        for(Element e : sList){
            String value = e.getTextTrim();
            String key = e.attributeValue("id");
            System.out.println("key:" + key + "----value:" + value);
        }
        
        
        //将xml转化为map
        Map<Integer, String> prodcutMap = xml2Map(path);
        for (Map.Entry<Integer, String> entry : prodcutMap.entrySet()) {
            System.out.println("键= " + entry.getKey() + " and 值= " + entry.getValue());
        }
    }
    
    //将xml转化为map
    public static Map<Integer, String> xml2Map(String path) throws Exception{
        Map<Integer, String> productMap = new HashMap<Integer, String>();
        Document  document = new SAXReader().read(path);
        Element root = document.getRootElement();                             //获取根节点
        Iterator<Element> it = root.element("product").elementIterator();   //获取根节点下的子节点product下面的所有节点
        while(it.hasNext()){
            Element e = (Element) it.next();
            Integer key = Integer.parseInt(e.attributeValue("id"));
            String value = e.getTextTrim();
            productMap.put(key, value);
        }
        
        return productMap;
    }
}

//可以将上面xml2Map改装,传入节点名字,nodeName,然后输出map

 

console:

转载于:https://www.cnblogs.com/tenWood/p/7282755.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值