java解析xml工具_Java XML解析工具 dom4j介绍及使用实例

Java XML解析工具 dom4j介绍及使用实例

dom4j介绍

dom4j是一个简单的开源库,用于处理XML、 XPath和XSLT,它基于Java平台,使用Java的集合框架,全面集成了DOM,SAX和JAXP。

dom4j的使用

下载了dom4j项目之后,解压缩,将其jar包(我的当前版本叫做dom4j-1.6.1.jar)加入class path下面。

(Properties->Java Build Path -> Add External JARs...)。

之后就可以使用其提供的API进行编程。

程序实例1

第一个程序,用Java代码生成xml文档,代码如下:

48304ba5e6f9fe08f3fa1abda7d326ab.png

packagecom.example.xml.dom4j;importjava.io.FileOutputStream;importjava.io.FileWriter;importorg.dom4j.Document;importorg.dom4j.DocumentHelper;importorg.dom4j.Element;importorg.dom4j.io.OutputFormat;importorg.dom4j.io.XMLWriter;/*** dom4j框架学习 使用dom4j框架创建xml文档并输出保存

**/

public classDom4JTest1

{public static void main(String[] args) throwsException

{//第一种方式:创建文档,并创建根元素//创建文档:使用了一个Helper类

Document document =DocumentHelper.createDocument();//创建根节点并添加进文档

Element root = DocumentHelper.createElement("student");

document.setRootElement(root);//第二种方式:创建文档并设置文档的根元素节点

Element root2 = DocumentHelper.createElement("student");

Document document2=DocumentHelper.createDocument(root2);//添加属性

root2.addAttribute("name", "zhangsan");//添加子节点:add之后就返回这个元素

Element helloElement = root2.addElement("hello");

Element worldElement= root2.addElement("world");

helloElement.setText("hello Text");

worldElement.setText("world text");//输出//输出到控制台

XMLWriter xmlWriter = newXMLWriter();

xmlWriter.write(document);//输出到文件//格式

OutputFormat format = new OutputFormat(" ", true);//设置缩进为4个空格,并且另起一行为true

XMLWriter xmlWriter2 = newXMLWriter(new FileOutputStream("student.xml"), format);

xmlWriter2.write(document2);//另一种输出方式,记得要调用flush()方法,否则输出的文件中显示空白

XMLWriter xmlWriter3 = new XMLWriter(new FileWriter("student2.xml"),

format);

xmlWriter3.write(document2);

xmlWriter3.flush();//close()方法也可以

}

}

48304ba5e6f9fe08f3fa1abda7d326ab.png

程序Console输出:

生成的一个xml文档:

48304ba5e6f9fe08f3fa1abda7d326ab.png

hello Text

world text

48304ba5e6f9fe08f3fa1abda7d326ab.png

程序实例2

程序实例2,读入xml文档并分析,将其内容输出。

首先,待分析的文档如下:

48304ba5e6f9fe08f3fa1abda7d326ab.png

hello Text1

hello Text2

hello Text3

world text1

world text2

world text3

48304ba5e6f9fe08f3fa1abda7d326ab.png

Java代码:

48304ba5e6f9fe08f3fa1abda7d326ab.png

packagecom.example.xml.dom4j;importjava.io.File;importjava.util.Iterator;importjava.util.List;importjavax.xml.parsers.DocumentBuilder;importjavax.xml.parsers.DocumentBuilderFactory;importorg.dom4j.Document;importorg.dom4j.Element;importorg.dom4j.io.DOMReader;importorg.dom4j.io.SAXReader;/*** dom4j框架学习: 读取并解析xml

*

**/

public classDom4JTest2

{public static void main(String[] args) throwsException

{

SAXReader saxReader= newSAXReader();

Document document= saxReader.read(new File("students.xml"));//获取根元素

Element root =document.getRootElement();

System.out.println("Root: " +root.getName());//获取所有子元素

List childList =root.elements();

System.out.println("total child count: " +childList.size());//获取特定名称的子元素

List childList2 = root.elements("hello");

System.out.println("hello child: " +childList2.size());//获取名字为指定名称的第一个子元素

Element firstWorldElement = root.element("world");//输出其属性

System.out.println("first World Attr: "

+ firstWorldElement.attribute(0).getName() + "="

+ firstWorldElement.attributeValue("name"));

System.out.println("迭代输出-----------------------");//迭代输出

for (Iterator iter =root.elementIterator(); iter.hasNext();)

{

Element e=(Element) iter.next();

System.out.println(e.attributeValue("name"));

}

System.out.println("用DOMReader-----------------------");

DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();

DocumentBuilder db=dbf.newDocumentBuilder();//注意要用完整类名

org.w3c.dom.Document document2 = db.parse(new File("students.xml "));

DOMReader domReader= newDOMReader();//将JAXP的Document转换为dom4j的Document

Document document3 =domReader.read(document2);

Element rootElement=document3.getRootElement();

System.out.println("Root: " +rootElement.getName());

}

}

48304ba5e6f9fe08f3fa1abda7d326ab.png

代码运行后输出:

48304ba5e6f9fe08f3fa1abda7d326ab.png

Root: students

total child count:6hello child:3first World Attr: name=wangwu

迭代输出-----------------------

lisi

lisi2

lisi3

wangwu

wangwu2

null

用DOMReader-----------------------

Root: students

48304ba5e6f9fe08f3fa1abda7d326ab.png

参考资料

圣思园张龙老师XML视频教程。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值