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:

dom4j使用的小例子的更多相关文章

  1. Dom4j的一个小例子,用于解析xml文件的元素获取方式(转)

    import java.io.File; import java.io.IOException; import javax.xml.parsers.ParserConfigurationExcepti ...

  2. Dom4j的一个小例子,用于解析xml文件的元素获取方式

    import java.io.File; import java.io.IOException; import javax.xml.parsers.ParserConfigurationExcepti ...

  3. java操作xml的一个小例子

    最近两天公司事比较多,这两天自己主要跟xml打交道,今天更一下用java操作xml的一个小例子. 原来自己操作xml一直用这个包:xstream-1.4.2.jar.然后用注解的方式,很方便,自己只要 ...

  4. Spring和Hibernate结合的一个小例子

    1.新建一个SpringHibernate的maven项目 2.pom文件的依赖为 <dependency> <groupId>junit</groupId> &l ...

  5. springmvc入门的第一个小例子

    今天我们探讨一下springmvc,由于是初学,所以简单的了解一下 springmvc的流程,后续会持续更新... 由一个小例子来简单的了解一下 springmvc springmvc是spring框 ...

  6. java即时通信小例子

    学习java一段时间了,今天写来一个即时通信的小例子练手在其过程中也学到了一些知识拿出来和大家分享,请路过的各位大神多多赐教... 好了下面讲一下基本的思路: 首先,编写服务器端的程序,简单点说吧就是 ...

  7. Runtime的几个小例子(含Demo)

    一.什么是runtime(也就是所谓的“运行时”,因为是在运行时实现的.)           1.runtime是一套底层的c语言API(包括很多强大实用的c语言类型,c语言函数);  [runti ...

  8. bootstrap 模态 modal 小例子

    bootstrap 模态 modal  小例子 <html> <head> <meta charset="utf-8" /> <title ...

  9. INI配置文件分析小例子

    随手写个解析INI配置字符串的小例子 带测试 #include <iostream> #include <map> #include <string> #inclu ...

随机推荐

  1. Prism 4 文档 ---第8章 导航

        作为同用户具有丰富的交互的客户端应用程序,它的用户界面(UI)将会持续不断的更新来反映用户工作的当前的任务和数据.用户界面可以进行一段时间相当大的变化作为用户交互的应用程序中完成各种任务.通过 ...

  2. hystrix -hystrix常用配置介绍

    配置官网介绍地址:https://github.com/Netflix/Hystrix/wiki/Configuration hystrix.command.default.execution.iso ...

  3. 剑指offer--48.机器人的运动范围

    这道题不是要求走一趟最多走多少,而是最多走多少,WA几次才想通. ------------------------------------------------------------------- ...

  4. spinlock变量没有初始化

    http://blog.csdn.net/longwang155069/article/details/52224284

  5. MQTT再学习 -- 漫谈MQTT协议

    先占楼,稍后再讲.先搭建好MQTT服务器和客户端

  6. MDN搜索结果自动跳转中文地址

    MDN社区(即Mozilla开发者社区)具有很多高质量中英文文档.它是我开发时遇到概念模糊的地方经常访问的网站.因为默认搜索一些代码,优先显示的都是英文.但是恰恰这些显示的英文文档是有中文的.每次都是 ...

  7. python爬虫入门(4)-补充知识:XPath 教程(转自w3school)

    http://www.w3school.com.cn/xpath/index.asp 参考手册:http://www.w3school.com.cn/xpath/xpath_functions.asp ...

  8. python pass关键字神奇吗

    参考文献:http://blog.sina.com.cn/s/blog_76e94d210100vz3e.html 1.空语句 do nothing2.保证格式完整3.保证语义完整 好吧!它什么也没干 ...

  9. vue 之 vue-router

    官方文档 // 0. 如果使用模块化机制编程,导入Vue和VueRouter,要调用 Vue.use(VueRouter) // 1. 定义(路由)组件. // 可以从其他文件 import 进来 c ...

  10. I.MX6 Linux eGTouch TouchScreen porting

    I.MX6 Linux eGTouch TouchScreen porting 一.Download Driver: http://www.eeti.com.tw/drivers_Linux.html ...