1、user.xml如下:
<?xml version="1.0" encoding="UTF-8"?>
<users>
<user>
<name>zhangsan</name>
<age>24</age>
<sex>man</sex>
</user>
<user>
<name>lisi</name>
<age>23</age>
<sex>man</sex>
</user>
</users>
2.读取上面xml的文件如下:
public void readXml(String path) throws DocumentException, FileNotFoundException{
SAXReader reader=new SAXReader();
Document doc=reader.read(new FileInputStream("D:/myeclipseworkspace/dom4j_xml/src/mxr/xml/test/user.xml"));
Element root=doc.getRootElement();
for (Iterator iter = root.elementIterator(); iter.hasNext();) {
Element element = (Element) iter.next();
System.out.println(element.getName());
for(Iterator it=element.elementIterator();it.hasNext();){
Element e=(Element) it.next();
System.out.println(e.getName()+"="+e.getText());
}
}
}
3、生成一个xml文件的代码如下:
public void createXML() throws IOException{
//用工厂类创建一个document实例
Document doc=DocumentHelper.createDocument();
Element root=doc.addElement("books");
//添加注释
root.addComment("this is a book about xml");
//添加子节点book
Element book=root.addElement("book");
//为book节点添加bookname属性
Element bookname=book.addElement("bookname");
//设置bookname的值
bookname.setText("java && java");
//为book添加author属性
Element author=book.addElement("author");
//为author 属性设置数值
author.setText("nobody&& nobody");
//为book 添加price 属性
Element price=book.addElement("price");
//设置price的值
price.setText("28.5");
FileWriter out=new FileWriter("books.xml");
//格式化输出,类型IE浏览一样
OutputFormat format=OutputFormat.createPrettyPrint();
format.setEncoding("utf-8");
XMLWriter writer=new XMLWriter(out,format);
writer.write(doc);
writer.close();
System.out.println("创建了xml文件");
}
4、最终生成的xml效果如下:
<?xml version="1.0" encoding="utf-8" ?>
- <books>
- <!--
this is a book about xml --> <script></script>
- <book>
<bookname> java && java</bookname>
<author> nobody&& nobody</author>
<price> 28.5</price>
</book>
</books>
<?xml version="1.0" encoding="UTF-8"?>
<users>
<user>
<name>zhangsan</name>
<age>24</age>
<sex>man</sex>
</user>
<user>
<name>lisi</name>
<age>23</age>
<sex>man</sex>
</user>
</users>
2.读取上面xml的文件如下:
public void readXml(String path) throws DocumentException, FileNotFoundException{
SAXReader reader=new SAXReader();
Document doc=reader.read(new FileInputStream("D:/myeclipseworkspace/dom4j_xml/src/mxr/xml/test/user.xml"));
Element root=doc.getRootElement();
for (Iterator iter = root.elementIterator(); iter.hasNext();) {
Element element = (Element) iter.next();
System.out.println(element.getName());
for(Iterator it=element.elementIterator();it.hasNext();){
Element e=(Element) it.next();
System.out.println(e.getName()+"="+e.getText());
}
}
}
3、生成一个xml文件的代码如下:
public void createXML() throws IOException{
//用工厂类创建一个document实例
Document doc=DocumentHelper.createDocument();
Element root=doc.addElement("books");
//添加注释
root.addComment("this is a book about xml");
//添加子节点book
Element book=root.addElement("book");
//为book节点添加bookname属性
Element bookname=book.addElement("bookname");
//设置bookname的值
bookname.setText("java && java");
//为book添加author属性
Element author=book.addElement("author");
//为author 属性设置数值
author.setText("nobody&& nobody");
//为book 添加price 属性
Element price=book.addElement("price");
//设置price的值
price.setText("28.5");
FileWriter out=new FileWriter("books.xml");
//格式化输出,类型IE浏览一样
OutputFormat format=OutputFormat.createPrettyPrint();
format.setEncoding("utf-8");
XMLWriter writer=new XMLWriter(out,format);
writer.write(doc);
writer.close();
System.out.println("创建了xml文件");
}
4、最终生成的xml效果如下:
<?xml version="1.0" encoding="utf-8" ?>
- <books>
- <!--
this is a book about xml --> <script></script>
- <book>
<bookname> java && java</bookname>
<author> nobody&& nobody</author>
<price> 28.5</price>
</book>
</books>