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:

dom4j使用的小例子的更多相关文章
- Dom4j的一个小例子,用于解析xml文件的元素获取方式(转)
import java.io.File; import java.io.IOException; import javax.xml.parsers.ParserConfigurationExcepti ...
- Dom4j的一个小例子,用于解析xml文件的元素获取方式
import java.io.File; import java.io.IOException; import javax.xml.parsers.ParserConfigurationExcepti ...
- java操作xml的一个小例子
最近两天公司事比较多,这两天自己主要跟xml打交道,今天更一下用java操作xml的一个小例子. 原来自己操作xml一直用这个包:xstream-1.4.2.jar.然后用注解的方式,很方便,自己只要 ...
- Spring和Hibernate结合的一个小例子
1.新建一个SpringHibernate的maven项目 2.pom文件的依赖为 <dependency> <groupId>junit</groupId> &l ...
- springmvc入门的第一个小例子
今天我们探讨一下springmvc,由于是初学,所以简单的了解一下 springmvc的流程,后续会持续更新... 由一个小例子来简单的了解一下 springmvc springmvc是spring框 ...
- java即时通信小例子
学习java一段时间了,今天写来一个即时通信的小例子练手在其过程中也学到了一些知识拿出来和大家分享,请路过的各位大神多多赐教... 好了下面讲一下基本的思路: 首先,编写服务器端的程序,简单点说吧就是 ...
- Runtime的几个小例子(含Demo)
一.什么是runtime(也就是所谓的“运行时”,因为是在运行时实现的.) 1.runtime是一套底层的c语言API(包括很多强大实用的c语言类型,c语言函数); [runti ...
- bootstrap 模态 modal 小例子
bootstrap 模态 modal 小例子 <html> <head> <meta charset="utf-8" /> <title ...
- INI配置文件分析小例子
随手写个解析INI配置字符串的小例子 带测试 #include <iostream> #include <map> #include <string> #inclu ...
随机推荐
- vue 错误记录
1.错误信息: You may use special comments to disable some warnings.Use // eslint-disable-next-line to ign ...
- asp下去除数组中重复的项的方法
<%Function MoveR(Rstr) Dim i,SpStr SpStr = Split(Rstr,",") For i = 0 To Ubound(Spstr) I ...
- 配置mysql 允许远程连接
今天折腾了好常时间远程连接mysql ,在云服务器里面打开了3306端口,使用了service mysql status查看到mysql进程正常. netstat -anp | grep mysql ...
- ionic安装插件常用命令
常见插件查找网站: http://ngcordova.com/docs/plugins http://cordova.apache.org/plugins/ $ ionic plugin list / ...
- Intellij IDEA创建包(package)问题解决方案
问题 在使用IDEA创建包时会出现这样一种场景,就是当一个空包很长时,比如com.secbro.drools.model.这个时候如果你想给drools或model创建同级的包,你会发现,默认创建的包 ...
- js 以函数名作为参数动态执行 函数
function myFunc() { console.log(11111); } test("myFunc"); function test(funcName) { if(typ ...
- HDU 1452
http://acm.hdu.edu.cn/showproblem.php?pid=1452 原来真心没见过这种题,不会做,非常帅 gcd(a,b)==1 && s(a,b)==s(a ...
- IOS开发 基础框架(Fondation Framework)的线程安全
有一种误解,认为基础框架(Foundation framework)是线程安全的,而Application Kit是非线程安全的.不幸的是,这是一个总的概括,从而造成一点误导.每个框架都包含了线程安全 ...
- 我为什么想并且要学习Scala
为什么学习函数式编程 在阅读DDD巨著<Patterns, Principles, and Practices of Domain-Driven Design>的过程中,Scott在第5章 ...
- Git钩子:自定义你的工作流
Git钩子是在Git仓库中特定事件发生时自动运行的脚本.它可以让你自定义Git内部的行为,在开发周期中的关键点触发自定义的行为. Git钩子最常见的使用场景包括推行提交规范,根据仓库状态改变项目环境, ...