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. 为eclipse EE(汉化版) 配置Tomcat服务器

    为eclipse EE(汉化版) 配置Tomcat服务器   很多小朋友在初次使用eclipse进行web开发的时候,很是蒙蔽.以前都是文本编辑器写好,做好目录结构,往tomcat下一扔,重启,搞定. ...

  2. LeetCode OJ:Word Pattern(单词模式)

    Given a pattern and a string str, find if str follows the same pattern. Here follow means a full mat ...

  3. 在关于原生js获取getAttribute如果是src的一点注意事项

    最近抽空把seajs看完了 他们在fix代码的时候,写明在某浏览器下的情况和官方文档出处,这样有据可查.太赞了 顺便把我想要这段摘出来 可以直接dom.src,但是在ie6-7中是不支持的,并且在ge ...

  4. New Concept English three(14)

    06:55:29 32w/m 45words. There was a time when the owners of shop and businesses in Chicago had to pa ...

  5. js 删除数组的指定元素

    //为数组对象增加相应的的方法,直接调用数组的removeByValue方法即可删除指定元素 Array.prototype.removeByValue = function (val) { for ...

  6. postfix基于客户端的访问控制

    1.基于客户端的访问控制概览 postfix内置了多种反垃圾邮件的机制,其中就包括“客户端”发送邮件限制.客户端判别机制可以设定一系列客户信息的判别条件: smtpd_client_restricti ...

  7. HBase数据存储

    HRegionServer  HBase的数据文件都存储在HDFS上,格式主要有两种: - HFile:HBase中KeyValue数据的存储格式,HFile是Hadoop的二进制文件,实际上Sto ...

  8. height 与 min-height 的继承

    min-height: inherit; 继承父元素的 min-height: 80px; 但,不能继承父元素的 height: 200px; height: inherit; 能继承父元素的: he ...

  9. Aria2 - OS X 下载百度云资源神器

    官网介绍: (Aria2 is a light weight multi-protocol & multi-source command-line download utility. It s ...

  10. Vmware无法正常打开

    VMware的问题 VMware总是搞一些事情,比如隔三差五就打不开了.那么怎么办呢?以前总要把Vm的安装程序重新开始一遍,然后选择修复 但是呢?这样太麻烦了,前几月做web开发的时候接触了Apach ...