3. Jaxp中DOM解析步骤
在Jaxp中,获得document对象、将document对象写入xml文件 的过程是固定的,具体如下:
3.1 获得Document的固定步骤
//获得一个负责生产DocumentBuilder对象的工厂实例
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
//通过工厂获得一个DocumentBuilder对象,DocumentBuilder可以获得Document对象
DocumentBuilder builder = factory.newDocumentBuilder();
//获得document对象
Document document = builder.parse(fileName);
/*
* 下面可以填写具体解析步骤
*/
3.2 将document对象写入xml文件的固定步骤
//得到TransformerFactory工厂实例
TransformerFactory tff = TransformerFactory.newInstance();
//通过工厂得到一个转换器
Transformer tf = tff.newTransformer();
//设置编码
tf.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
//设置格式
//tf.setOutputProperty(OutputKeys.INDENT, "yes");
//把一个dom模型转换成对应的xml文件
tf.transform(new DOMSource(document), new StreamResult(fileName));
4. Jaxp中SAX解析步骤
//1.获取解析工厂
SAXParserFactory factroy = SAXParserFactory.newInstance();
//2.使用工厂创建SAX解析器
SAXParser saxParser = factroy.newSAXParser();
//3.开始解析 传入解析文件,重写解析事件
saxParser.parse(file, new DefaultHandler(){
startDocument() ---> 解析器发现了文档的开始标签
endDocument() ---> 解析器发现了文档结束标签
startElement() ---> 解析器发现了一个起始标签
character() ---> 解析器发现了标签里面的文本值
endElement() ---> 解析器发现了一个结束标签
});
注意:SAX只能解析,不能修改!!!
5. Dom4J解析具体步骤
Dom4j是一个简单、灵活的开放源代码的库。Dom4j是由早期开发JDOM的人分离出来而后独立开发的。与JDOM不同的是,dom4j使用接口和抽象基类,虽然Dom4j的API相对要复杂一些,但它提供了比JDOM更好的灵活性。
Dom4j是一个非常优秀的Java XML API,具有性能优异、功能强大和极易使用的特点。现在很多软件采用的Dom4j,例如Hibernate,包括sun公司自己的JAXP也用了Dom4j。
使用Dom4j开发,需下载dom4j相应的jar文件,使用时需要导包。
5.1 获得Document的固定步骤
//获得一个SAXReader对象
SAXReader reader = new SAXReader();
File file = new File(filePath);
//读取这个要解析的xml文件
Document document = reader.read(file);
//获得document中的根节点
Element rootElement = document.getRootElement();
/*
* 下面可以填写具体解析步骤
*/
5.2 将document对象写入xml文件的固定步骤
OutputFormat format = OutputFormat.createPrettyPrint();
XMLWriter xw = new XMLWriter(new FileOutputStream(new File(file)),format);
xw.write(document);
xw.flush();
xw.close();
dom解析:
<?xml version="1.0" encoding="UTF-8"?>
<employees>
<employee id="1" depName="教学部">
<name>tom</name>
<age>20</age>
<gender>male</gender>
<email>tom@briup.com</email>
<salary>8k</salary>
</employee>
<employee id="2" depName="教学部">
<name>jack</name>
<age>25</age>
<gender>male</gender>
<email>jack@briup.com</email>
<salary>10k</salary>
</employee>
<employee id="3" depName="市场部">
<name>rose</name>
<age>25</age>
<gender>female</gender>
<email>rose@briup.com</email>
<salary>5k</salary>
</employee>
<employee id="4" depName="教学管理部">
<name>mark</name>
<age>30</age>
<gender>male</gender>
<email>mark@briup.com</email>
<salary>5k</salary>
</employee>
</employees>
package com.briup.xml.chap02;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import com.briup.xml.chap01.Employee;
public class DomEmployee {
private static Employee employee;
private static List<Employee> list =
new ArrayList<Employee>();
public static void main(String[] args) {
DocumentBuilderFactory factory =
DocumentBuilderFactory.newInstance();
try {
DocumentBuilder builder = factory.newDocumentBuilder();
File file = new File("src/com/briup/xml/chap01/employee.xml");
Document document = builder.parse(file);
//直接读取employee标签,构建employee对象
NodeList nl = document.getElementsByTagName("employee");
for(int i=0;i<nl.getLength();i++) {
//遍历一个employee标签,构建一个employee对象
employee = new Employee();
//将node节点转换成element节点
Element e = (Element) nl.item(i);
int id = Integer.parseInt(e.getAttribute("id"));
String depName = e.getAttribute("depName");
employee.setId(id);
employee.setDepName(depName);
//获取employee节点所有子节点
//主要获取name,age,gender等一些元素节点,不需要文本节点
NodeList nl2 = e.getChildNodes();
for(int j=0;j<nl2.getLength();j++) {
if(nl2.item(j).getNodeType() == Node.ELEMENT_NODE) {
String name = nl2.item(j).getNodeName();
String value = nl2.item(j).getTextContent();
if("name".equals(name)) {
employee.setName(value);
}else if("age".equals(name)) {
employee.setAge(Integer.parseInt(value));
}else if("gender".equals(name)) {
employee.setGender(value);
}else if("email".equals(name)) {
employee.setEmail(value);
}else if("salary".equals(name)) {
employee.setSalary(value);
}
}
}
list.add(employee);
}
System.out.println(list.size());
} catch (Exception e) {
e.printStackTrace();
}
}
}
dom4j解析:
写xml:
package com.briup.xml.chap02;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
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.XMLWriter;
public class Dom4jTest {
public static void main(String[] args) {
Dom4jTest test = new Dom4jTest();
Document doc = test.generateDocument2();
File file = new File("src/com/briup/xml/chap02/student.xml");
test.saveDocument(doc, file);
}
public void saveDocument(Document doc,File file) {
//美化格式
OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding("utf-8");
try {
//按照字符流将xml输出到指定file文件中
OutputStream os= new FileOutputStream(file);
OutputStreamWriter osw = new OutputStreamWriter(os);
XMLWriter writer = new XMLWriter(osw, format);
//根据document内容按照xml格式输出
writer.write(doc);
writer.flush();
} catch (Exception e) {
e.printStackTrace();
}
}
/*
* <students>
* <student id='1'>
* <name>....</name>
* ....
* </student>
*</students>
*/
public Document generateDocument(){
//创建文档
Document document = DocumentHelper.createDocument();
//创建根节点
Element studentsElement = document.addElement("students");
//创建注释
studentsElement.addComment("this is comment");
//创建stu1子节点
Element stu1Element = studentsElement.addElement("student");
//stu1子节点添加属性
stu1Element.addAttribute("id","1");
Element nameElement = stu1Element.addElement("name");
nameElement.addText("tom");
Element ageElement = stu1Element.addElement("age");
ageElement.addText("18");
return document;
}
public Document generateDocument2() {
String str = "<?xml version='1.0' encoding='utf-8'?>"
+"<students><!--this is common-->"
+ "<student id='2'>"
+ "<name>jack</name><age>20</age>"
+ "</student></students>";
Document document = null;
try {
document = DocumentHelper.parseText(str);
} catch (DocumentException e) {
e.printStackTrace();
}
return document;
}
}
获取xml元素:
<?xml version="1.0" encoding="utf-8"?>
<students>
<!--this is common-->
<student id="2">
<name>jack</name>
<age>20</age>
</student>
</students>
package com.briup.xml.chap02;
import java.io.File;
import java.util.Iterator;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
/*
* 方法readDocumentByIterator()提供一种解析xml文件方法
* 每个Element通过elementIterator()和attributeIterator获取
* 其子元素和属性的迭代器
*/
public class Dom4jTest2 {
private File file;
public Dom4jTest2(File file) {
this.file = file;
}
public static void main(String[] args) {
Dom4jTest2 test = new Dom4jTest2(
new File("src/com/briup/xml/chap02/student.xml"));
test.readDocumentByIterator();
//将xml转换成string类型
SAXReader reader = new SAXReader();
try {
Document document = reader.read
(new File("src/com/briup/xml/chap02/student.xml"));
String docXmlText = document.asXML();
System.out.println(docXmlText);
System.out.println("-------------docXmlText----------");
Element root = document.getRootElement();
String rootXmlText = root.asXML();
System.out.println(rootXmlText);
System.out.println("-------------rootXmlText----------");
Element son = root.element("student");
String sonXmlText = son.asXML();
System.out.println(sonXmlText);
System.out.println("-------------sonXmlText-----------");
} catch (Exception e) {
e.printStackTrace();
}
}
public Document getDocument() {
SAXReader reader = new SAXReader();
Document document = null;
try {
document = reader.read(file);
} catch (DocumentException e) {
e.printStackTrace();
}
return document;
}
/*
* 该方法只是枚举了两层,如果一直读取需要进行迭代循环
*/
public void readDocumentByIterator() {
Element root = getDocument().getRootElement();
//枚举根节点下所有子节点
for(Iterator ie = root.elementIterator();ie.hasNext();) {
System.out.println("========");
Element element = (Element) ie.next();
System.out.println(element.getName());
//枚举属性
for(Iterator ia = element.attributeIterator();ia.hasNext();) {
Attribute attribute = (Attribute) ia.next();
System.out.println(attribute.getName()
+"="+attribute.getValue());
}
//枚举当前节点下所有子节点
for(Iterator ieson = element.elementIterator();ieson.hasNext();) {
Element son = (Element) ieson.next();
System.out.println(son.getName()
+"="+son.getText());
}
}
}
}