1.XML解析技术概述
(1)XML解析方式分为两种:dom和sax
dom:(Document Object Model, 即文档对象模型) 是 W3C 组织推荐的处理 XML 的一种标准方式。
sax: (Simple API for XML) 不是官方标准,但它是 XML 社区事实上的标准,几乎所有的 XML 解析器都支持它。
(2)XML解析器(软件/实现类)
Crimson(SUN)、Xerces(IBM->APACHE) 、Aelfred2(DOM4J开源组织)
(3)XML解析技术API/开发包(在使用时,一定要导入jar包dom4j-1.6.1.jar,然后右键“Butid Path”)
Jaxp(SUN/dom/sax)、
Jdom、
dom4j
以上三种,应该说逐渐递增的好用吧,最简单大莫如dom4j。
2.
DOM4j中,获得Document对象的方式有三种:SAXReader reader = new SAXReader();
Document document = reader.read(new File("input.xml"));
(2).解析XML形式的文本,得到document对象String text = "<members></members>";
Document document = DocumentHelper.parseText(text);
(3).主动创建document对象(创建)Element root = document.addElement("members");
3.将文档写入XML文件
(1)文档中全为英文,不设置编码,直接写入的形式.
XMLWriter writer = new XMLWriter(new FileWriter("output.xml")); document.add(rootElement);
writer.write(document);
writer.flush();
writer.close();
(2)文档中含有中文,设置编码格式写入的形式.
OutputFormat format = OutputFormat.createCompactFormat();//以紧凑的方式
OutputFormat of = OutputFormat.createPrettyPrint();//有换行的模式
OutputStream os = new FileOutputStream("src/cn/itcast/xml/dom4j/car.xml");
XMLWriter xmlWriter = new XMLWriter(os,format);
或者这样:(输出中文字符的要设置编码方式“utf-8”)
XMLWriter writer = new XMLWriter(new OutputStreamWriter(new FileOutputStream(filePath),"UTF-8"),OutputFormat.createPrettyPrint());
xmlWriter.write(document);
xmlWriter.close();
4.(1)CRUD的含义:CreateReadUpdateDelete增删查改
(2)XML解析器有二类,分别是DOM和SAX。//firstCarPriceElement.getParent().remove(firstCarPriceElement):从直接父元素删除直接子元素
5.代码练习(实现对xml的crud操作,不过在对xml的cud中都蕴含着r,所以并没有单独写r)
xml源文件(student.xml):
<?xml version="1.0" encoding="UTF-8"?>
<Person>
<Student num="2014072201" >
<name>一叶扁舟</name>
<age>22</age>
<profession>软件工程</profession>
</Student>
<Student num="2014072202">
<name>努力</name>
<age>21</age>
<profession>软件工程</profession>
<sex>女</sex>
</Student>
</Person>
测试代码(Demo.java):
package cn.wwh.www.xml.dom4j;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.List;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import org.junit.Test;
/**
*类的作用:采用xml的dom4j解析xml文件,并能够实现(CRUD操作)
*注意:
* 1.采用xpp3reader的解析方式
XPP3Reader xpp3Reader = new XPP3Reader();
Document document = xpp3Reader.read(new File(filePath));
不知道为啥不行????
*
*
*@author 一叶扁舟
*@version 1.0
*@创建时间: 2014-7-22 上午10:39:47
*/
public class Demo2 {
private final static String filePath = "src/cn/wwh/www/xml/dom4j/student.xml";
// 实现xml属性和元素的增加
@Test
public void create() throws Exception{
// 采用SAXReader的解析方式:
SAXReader saxReader = new SAXReader();
Document document = saxReader.read(new File(filePath));
Element root = document.getRootElement();
// 获取Person下的第一元素Student
Element firstElement = (Element) root.elements().get(0);
// 为第一个Student,添加一个属性id,属性值为:"19920101"
firstElement.addAttribute("id", "19920101");
firstElement.addElement("sex").setText("男");
// 输出根节点下的所有东西
List<Element> elements = root.elements();
System.out.println("根节点下有:"+elements.size()+"元素");
for (Element e : elements) {
// 获取student的属性
Attribute attribute = e.attribute(0);
System.out.println("标签名:"+e.getName());
System.out.println(e.getName()+"属性
值:"+attribute.getData()+"\n" +
"attribute.getName():"+attribute.getName()+"\n" +
"attribute.getText():"+attribute.getText());
// 获取所有的子标签
List<Element> child = e.elements();
for (Element element : child) {
System.out.println(element.getName()+":"+
element.getText());
}
}
// 将数据写会到xml文件中
OutputStream os = new FileOutputStream(filePath);
OutputFormat of = OutputFormat.createPrettyPrint();
OutputStreamWriter osw = new OutputStreamWriter(os,"UTF-8");
XMLWriter xmlWrite = new XMLWriter(osw, of);
xmlWrite.write(document);
xmlWrite.flush();
xmlWrite.close();
}
// 实现对xml文件的修改
@Test
public void update() throws Exception{
SAXReader saxReader = new SAXReader();
Document document = saxReader.read(new File(filePath));
Element root =
document.getRootElement();
System.out.println(root.getName());
// 获取第一个Student
Element firstStudent = (Element) root.elements().get(0);
Element sex = firstStudent.element("sex");
// firstStudent.element("sex").setText("女");
System.out.println(sex.getName());
sex.setText("男");
// 将数据写进xml文件中
OutputStream os = new FileOutputStream(filePath);
OutputStreamWriter osw = new OutputStreamWriter(os,"UTF-8");
OutputFormat of = OutputFormat.createPrettyPrint();
XMLWriter xmlWriter = new XMLWriter(osw,of);
xmlWriter.write(document);
xmlWriter.flush();
xmlWriter.close();
}
// 实现对xml的删除操作
@Test
public void delete() throws Exception{
SAXReader reader = new SAXReader();
Document document = reader.read(new File(filePath));
Element element = document.getRootElement();
Element firstStudent = (Element) element.elements().get(0);
// 获取Student 的属性id
Attribute attribute = firstStudent.attribute("id");
System.out.println(attribute.getName());
firstStudent.remove(attribute);
// firstStudent.remove();
// 删除第一Student下sex的元素:
Element sex = firstStudent.element("sex");
sex.getParent().remove(sex);
// sex.remove(sex);
// 将数据写回文件中
XMLWriter writer = new XMLWriter(new OutputStreamWriter(new FileOutputStream(filePath),"UTF-8"),OutputFormat.createPrettyPrint());
writer.write(document);
writer.flush();
writer.close();
}
}
代码测试效果图(仅仅显示的create的效果图):
原文地址:http://blog.csdn.net/u011662320/article/details/38048205