dom4j解析xml文件
2015-12-01 17:24
375 查看
dom4j解析xml文件
dom4j是一个Java的XML API,类似于jdom,用来读写XML文件的。dom4j是一个非常非常优秀的Java XML API,具有性能优异、功能强大和极易用使用的特点,同时它也是一个开放源代码的软件,可以在SourceForge上找到它.
对主流的Java XML API进行的性能、功能和易用性的评测,dom4j无论在那个方面都是非常出色的。如今你可以看到越来越多的Java软件都在使用dom4j来读写XML,例如Hibernate,包括sun公司自己的JAXM也用
了Dom4j。
官网下载: http://www.dom4j.org/dom4j-1.6.1
以下为测试代码
package xml;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Iterator;
import java.util.List;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
/**
* xml学习
* @author wangkj
*
*/
public class xmlStudy {
public static void main(String[] args) throws DocumentException, IOException {
Document doc=getDocument();
if(null ==doc)
return;
Element root = doc.getRootElement();
if(null==root)
return;
//读取xml文件,遍历节点
long t1=System.currentTimeMillis();
listNodes(root);
// elementMethod(root);
// writeXml(doc);
// strToXml(doc);
long t2 = System.currentTimeMillis();
System.out.println(t2-t1);
// createXml();
}
/**
* 获得Document对象的三种方式
* @return
* @throws DocumentException
*/
public static Document getDocument() throws DocumentException{
Document doc=null;
//读取xml文件,获得Document对象
SAXReader reader = new SAXReader();
// doc= reader.read(new File("src\\xml\\test.xml"));
doc= reader.read(new File("src\\xml\\job1.xml"));
// String txt=doc.asXML();//读取的xml装换为文本
// System.out.println(txt);
//解析xml形式的文本,获得Document对象
// String xmlTxt = "<class></class>";
// doc = DocumentHelper.parseText(xmlTxt);
//创建Document对象
// doc = DocumentHelper.createDocument();
// Element root = doc.addElement("root");//创建根节点
return doc;
}
/**
* 读取xml文件,遍历当前节点下的所有子节点
* @param node
*/
@SuppressWarnings("unchecked")
public static void listNodes(Element node){
System.out.println("当前节点的名称:"+node.getName());
//获取当前节点的所有属性节点
List<Attribute> list = node.attributes();
//遍历属性节点
for(Attribute attr:list){
System.out.println("属性:"+attr.getName()
+"="+attr.getValue());
}
//获得文本节点
if(!node.getTextTrim().equals(""))
System.out.println("文本节点内容:"+node.getText());
//当前节点下子节点迭代器
Iterator<Element> it = node.elementIterator();
//遍历
while(it.hasNext()){
//获取某个子节点的对象
Element e = it.next();
//递归遍历
listNodes(e);
}
}
/**
*
* Element中element()核elements()方法
* @param node
*/
public static void elementMethod(Element node){
//获取node节点中,子节点元素名称为college的元素节点(只能获取第一个college元素节点)
Element e = node.element("college");
System.out.println(e.getName());
//获取college节点下,子节点元素名称为class的元素节点(第一个)
e = e.element("class");
//获取class节点下,子节点元素名称为student的所有元素
List<Element> stus = e.elements("student");
for(Element stu:stus)
System.out.println("第一个colleg节点下的第一个class节点下所有的节点元素的name属性值:"+stu.attributeValue("name"));
//获取node节点下的所有元素的节点
List<Element> elements = node.elements();
for(Element ee:elements)
System.out.println("根目录下的所有子元素的name属性值:"+ee.attributeValue("name"));
}
/**
* 写xml文件
* @param doc
* @throws IOException
*/
public static void writeXml(Document doc) throws IOException{
//紧凑格式
// OutputFormat format = OutputFormat.createCompactFormat();
//排版缩进格式
OutputFormat format = OutputFormat.createPrettyPrint();
//设置编码格式
format.setEncoding("UTF-8");
File file = new File("src\\xml\\test1.xml");
if(file.exists())
file.delete();
//制订了写出文件及编码格式
XMLWriter writer = new XMLWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8"), format);
//写入
writer.write(doc);
//刷新立即写入
writer.flush();
//关闭流
writer.close();
}
/**
* 字符串和xml之间的转换
* @param doc
* @throws DocumentException
* @throws IOException
*/
public static void strToXml(Document doc) throws DocumentException, IOException{
//xml文档转换为字符串
String strXml = doc.asXML();
System.out.println(strXml);
Element root = doc.getRootElement();
if(null == root)
return;
//xml根元素标签转换为字符串
String strRoot = root.asXML();
// System.out.println("root:"+strRoot);
String txt ="<A>" +"<a index='1'></a>"+"<a index='2'>a2</a>"+"</A>";
//转换为文档对象
doc = DocumentHelper.parseText(txt);
writeXml(doc);
}
/**
* 创建Document对象,往doc对象中添加元素节点,转换为xml文件
* @throws IOException
*/
public static void createXml() throws IOException{
Document doc = DocumentHelper.createDocument();
Element root = doc.addElement("ROOT");//创建根节点
//在根节点下添加属性和子节点等。。
//添加属性
root.addAttribute("name", "root");
root.addComment("这是根节点.");
Element child = root.addElement("child");
child.setText("文本内容。");//设置文本节点
child.setName("c");//设置节点的名称child--c
writeXml(doc);
}
}
2、xml测试文件
<?xml version="1.0" encoding="UTF-8"?>
<university name="pku">
<college name="c1">
<class name="class1" index="1">
<student name="stu1" sex='male' age="21" />
<student name="stu2" sex='female' age="20" />
<student name="stu3" sex='female' age="20" />
</class>
<class name="class2">
<student name="stu4" sex='male' age="19" />
<student name="stu5" sex='female' age="20" />
<student name="stu6" sex='female' age="21" />
</class>
</college>
<college name="c2">
<class name="class3">
<student name="stu7" sex='male' age="20" />
</class>
</college>
<college name="c3">
</college>
<manager name="m1">文本内容</manager>
</university>
相关文章推荐
- XML 与 JSON 优劣对比
- As3.0 xml + Loader应用代码
- Mootools 1.2教程(2) DOM选择器
- DOM 事件流详解
- 网马生成器 MS Internet Explorer XML Parsing Buffer Overflow Exploit (vista) 0day
- ext读取两种结构的xml的代码
- Dom在ajax技术中的作用说明
- C#针对xml基本操作及保存配置文件应用实例
- Ruby程序中创建和解析XML文件的方法
- asp下查询xml的实现代码
- sqlserver FOR XML PATH 语句的应用
- 使用sp_xml_preparedocument处理XML文档的方法
- EBS xml publisher中文乱码问题及解决办法
- C#中的Linq to Xml详解
- C#操作XML文件实例汇总
- javascript针对DOM的应用分析(三)
- Dom 是什么的详细说明
- javascript针对DOM的应用分析(五)
- SQL Server中的XML数据进行insert、update、delete