您的位置:首页 > 其它

dom4j创建了读取xml文件的方法总结

2017-01-04 10:37 519 查看
今天没事就查资料自学了一下dom4j读取和创建xml文件,做个笔记。以后用到的话可以参考一下

dom4j-1.6.1.jar 需要这个jar包

代码






package com.xml;

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

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;

public class XmlTest {

/**
* 创建xml
* @param filename
*/
public void printXml(String filename) {

// 创建稳当对象
Document el = DocumentHelper.createDocument();
// 添加元素,第一个根元素
Element em = el.addElement("employess");
// 在根元素下添加其它元素
Element ems = em.addElement("employe");
em.addAttribute("dept", "sale");

Element name = ems.addElement("userName");
name.setText("黄坤");
Element age = ems.addElement("age");
age.setText("100");
Element sex = ems.addElement("sex");
sex.setText("男");

// 创建写文档的对象
try {
File file=new File("config/"+filename);
OutputFormat ops = OutputFormat.createPrettyPrint();
XMLWriter xml = new XMLWriter(new FileOutputStream(file), ops);
xml.write(el);
xml.close();
} catch (IOException e) {
e.printStackTrace();
}

}

/**
* 解析xml
* @param args
* @throws DocumentException 
*/
public void parseXml(String name) throws DocumentException{
//第一种方式list解析
SAXReader saxreader=new SAXReader();
File file =new File("config/"+name);
Document document=saxreader.read(file);
Element root=document.getRootElement();
/*List<Element> childElement=root.elements();

List<Element> childs=childElement.get(0).elements();
for (Element element : childs) {
List<Attribute> att=element.attributes();
for (Attribute attribute : att) {
System.out.println(attribute.getValue());
}

System.out.println(element.getName()+":"+element.getText());
}*/

//第二种iterator解析
Iterator it=root.elementIterator();
while(it.hasNext()){
Element el=(Element) it.next();
Iterator chil=el.elementIterator();
while(chil.hasNext()){
Element node=(Element) chil.next();
System.out.println(node.getName()+"--------"+node.getText());
}
}

}

/**
* 追加新的内容
* @param args
* @throws IOException 
* @throws DocumentException
*/

public void appendXML(String name) throws IOException{
SAXReader reader=new SAXReader();
File file =new File("config/"+name);
Document document;
try {
document = reader.read(file);
Element employess=document.getRootElement(); //获取根节点
Element employe=employess.element("employe");
Element mingzu=employe.addElement("mingzu");
mingzu.setText("汉");
OutputFormat ops = OutputFormat.createPrettyPrint();
XMLWriter xml = new XMLWriter(new FileOutputStream(file), ops);
xml.write(document);
xml.close();

} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

public static void main(String[] args) throws DocumentException {

XmlTest xml = new XmlTest();
xml.parseXml("example3.xml");
/*try {
xml.appendXML("example3.xml");
} catch (IOException e) {
e.printStackTrace();
}*/
}

}


<?xml version="1.0" encoding="UTF-8"?>

<employess dept="sale">

  <employe>

    <userName>黄坤</userName>

    <age>100</age>

    <sex>男</sex>

  </employe>

</employess>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: