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>
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>
相关文章推荐
- C# WinForm 创建XML文件方法总结
- 使用dom4j四种读取xml文件的方法
- java读取XML文件的四种方法总结(必看篇)
- Dom4J读取XML文件时出现的connection timed out:connect Nested exception异常解决方法
- 简单创建XML文件和读取XML文件的方法
- 在C#中创建和读取XML文件的实现方法
- 使用Flash builder 4.6创建Flex手机项目读取XML文件方法
- 使用dom4j创建XML文件,并读取XML内容
- 使用dom4j四种读取xml文件的方法
- jdom和dom4j读取xml文件以及生成xml文件的方法
- 在java环境下读取xml文件的方法主要有4种:DOM、SAX、JDOM、JAXB、Dom4J
- 使用dom4j四种读取xml文件的方法
- SAX读取xml文件的方法总结
- 读取XML文件DOM4J方法
- DOM4J 学习总结之 读取XML文件指定元素内容
- dom4j从jar包中读取xml文件的方法
- 模拟Spring创建Bean和dom4j读取xml文件(视频学习-传智播客Spring2.5-03)
- 通过Dom4J操作(创建、读取和修改)XML文件
- 【方法总结】创建、读取、删除文件相关操作
- 使用XmlReader和XmlDocument 读取xml文件,使用总结