解析XML有五种方式:DOM、SAX、JDOM、DOM4J、PULL。XML在不同的语言里解析方式都是一样的,只不过实现的语法不同而已。基本的解析方式有两种,一种叫SAX,另一种叫DOM。SAX是基于事件流的解析,DOM是基于XML文档树结构的解析。
本文使用JAVA语言来实现DOM与SAX的XML文档生成与解析。 首先定义一个操作XML文档的接口XmlDocument 它定义了XML文档的建立与解析的接口。
package com.xqq.解析XML;
/** * 定义XML文档建立与解析的接口 * @author xqq */
public interface XmlDocument {
//建立XML文件
public void createXml(String fileName);
//解析XML文件
public void parserXml(String fileName);
}
1. DOM生成和解析XML文件
package com.xqq.解析XML;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class DomParseXml implements XmlDocument{
public void createXml(String fileName) {
//获取 DocumentBuilderFactory 的新实例
//使应用程序能够从 XML 文档获取生成 DOM 对象树的解析器
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
try {
//获取生成 DOM 对象树的解析器
DocumentBuilder builder = factory.newDocumentBuilder();
// Document 接口表示整个 HTML 或 XML 文档。从概念上讲,它是文档树的根,并提供对文档数据的基本访问。
Document document = builder.newDocument();
Element root = document.createElement("students");
document.appendChild(root);
Element student = document.createElement("student");
Element name = document.createElement("name");
name.appendChild(document.createTextNode("夏倩倩"));
student.appendChild(name);
Element age = document.createElement("age");
age.appendChild(document.createTextNode("20"));
student.appendChild(age);
Element sex = document.createElement("sex");
sex.appendChild(document.createTextNode("女"));
student.appendChild(sex);
root.appendChild(student);
// 获取 TransformerFactory 的新实例。
TransformerFactory transformerFactory = TransformerFactory.newInstance();
//创建执行从 Source 到 Result 的复制的新 Transformer。
Transformer transformer = transformerFactory.newTransformer();
//以 Document Object Model(DOM)树的形式充当转换 Source 树的持有者。
DOMSource source = new DOMSource(document);
transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
//向文本输出流打印对象的格式化表示形式
PrintWriter pw = new PrintWriter(new FileOutputStream(fileName));
//充当转换结果的持有者,可以为 XML、纯文本、HTML 或某些其他格式的标记
StreamResult result = new StreamResult(pw);
//将 XML Source 转换为 Result
transformer.transform(source, result);
System.out.println("生成XML文件成功!");
} catch (ParserConfigurationException e) {
System.out.println(e.getMessage());
} catch (TransformerConfigurationException e) {
System.out.println(e.getMessage());
} catch (FileNotFoundException e) {
System.out.println(e.getMessage());
} catch (TransformerException e) {
System.out.println(e.getMessage());
}
}
public void parserXml(String fileName) {
//工厂
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
try {
//获得解析器
DocumentBuilder builder = factory.newDocumentBuilder();
//根据文件名获得XML文件
Document document = builder.parse(fileName);
//获得所有的孩子节点
NodeList students = document.getChildNodes();
for(int i = 0; i < students.getLength(); i++){
Node student = students.item(i);
NodeList studentInfo = student.getChildNodes();
for(int j = 0; j < studentInfo.getLength(); j++){
Node node = studentInfo.item(j);
NodeList studentMeta = node.getChildNodes();
System.out.println("studentMeta len: " + studentMeta.getLength());
for(int k = 0; k < studentMeta.getLength(); k++){
System.out.println(studentMeta.item(k).getNodeName() +
": " + studentMeta.item(k).getTextContent());
}
}
}
System.out.println("解析完毕");
} catch (ParserConfigurationException e) {
System.out.println(e.getMessage());
} catch (SAXException e) {
System.out.println(e.getMessage());
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
public static void main(String[] args) {
DomParseXml domParseXml = new DomParseXml();
domParseXml.createXml("xqq.xml");
domParseXml.parserXml("xqq.xml");
}
}
运行结果:
生成XML文件成功!
#text:
name: 夏倩倩
#text:
age: 20
#text:
sex: 女
#text:
解析完毕
2. SAX生成和解析XML文件
package com.xqq.解析XML;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
/** * SAX文档解析 */
public class SaxDemo implements XmlDocument {
public void createXml(String fileName) {
System.out.println("<<" + fileName + ">>");
}
public void parserXml(String fileName) {
SAXParserFactory saxfac = SAXParserFactory.newInstance();
try {
SAXParser saxparser = saxfac.newSAXParser();
InputStream is = new FileInputStream(fileName);
saxparser.parse(is, new MySAXHandler());
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
SaxDemo saxDemo = new SaxDemo();
saxDemo.createXml("sax.xml");
saxDemo.parserXml("xqq.xml");
}
}
class MySAXHandler extends DefaultHandler {
boolean hasAttribute = false;
Attributes attributes = null;
public void startDocument() throws SAXException {
System.out.println("文档开始打印了");
}
public void endDocument() throws SAXException {
System.out.println("文档打印结束了");
}
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
if (qName.equals("employees")) {
return;
}
if (qName.equals("employee")) {
System.out.println(qName);
}
if (attributes.getLength() > 0) {
this.attributes = attributes;
this.hasAttribute = true;
}
}
public void endElement(String uri, String localName, String qName)
throws SAXException {
if (hasAttribute && (attributes != null)) {
for (int i = 0; i < attributes.getLength(); i++) {
System.out.println(attributes.getQName(0)
+ attributes.getValue(0));
}
}
}
public void characters(char[] ch, int start, int length)
throws SAXException {
System.out.println(new String(ch, start, length));
}
}
运行结果:
<<sax.xml>>
文档开始打印了
夏倩倩
20
女
文档打印结束了
3. JDOM生成和解析XML文件
package com.alisoft.facepay.framework.bean;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;
/** * JDOM 生成与解析XML文档 */
public class JDomDemo implements XmlDocument {
public void createXml(String fileName) {
Document document;
Element root;
root = new Element("employees");
document = new Document(root);
Element employee = new Element("employee");
root.addContent(employee);
Element name = new Element("name");
name.setText("ddvip");
employee.addContent(name);
Element sex = new Element("sex");
sex.setText("m");
employee.addContent(sex);
Element age = new Element("age");
age.setText("23");
employee.addContent(age);
XMLOutputter XMLOut = new XMLOutputter();
try {
XMLOut.output(document, new FileOutputStream(fileName));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void parserXml(String fileName) {
SAXBuilder builder=new SAXBuilder(false);
try {
Document document=builder.build(fileName);
Element employees=document.getRootElement();
List employeeList=employees.getChildren("employee");
for(int i=0;i < employeeList.size(); i++){
Element employee=(Element)employeeList.get(i);
List employeeInfo=employee.getChildren();
for(int j=0; j< employeeInfo.size(); i++){
System.out.println(((Element)employeeInfo.get(j)).getName()+":"+((Element)employeeInfo.get(j)).getValue());
}
}
} catch (JDOMException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
4. DOM4J生成和解析XML文件
package com.xqq.解析XML;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.Iterator;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
/** * Dom4j 生成XML文档与解析XML文档 */
public class Dom4jDemo implements XmlDocument {
public void createXml(String fileName) {
Document document = DocumentHelper.createDocument();
Element employees = document.addElement("employees");
Element employee = employees.addElement("employee");
Element name = employee.addElement("name");
name.setText("ddvip");
Element sex = employee.addElement("sex");
sex.setText("m");
Element age = employee.addElement("age");
age.setText("29");
try {
Writer fileWriter = new FileWriter(fileName);
XMLWriter xmlWriter = new XMLWriter(fileWriter);
xmlWriter.write(document);
xmlWriter.close();
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
public void parserXml(String fileName) {
File inputXml = new File(fileName);
SAXReader saxReader = new SAXReader();
try {
Document document = saxReader.read(inputXml);
Element employees = document.getRootElement();
for (Iterator i = employees.elementIterator(); i.hasNext()) {
Element employee = (Element) i.next();
for (Iterator j = employee.elementIterator(); j.hasNext()) {
Element node = (Element) j.next();
System.out.println(node.getName() + ":" + node.getText());
}
}
} catch (DocumentException e) {
System.out.println(e.getMessage());
}
System.out.println("dom4j parserXml");
}
}
5. PULL生成和解析XML文件
//读取到xml的声明返回数字0 START_DOCUMENT;
//读取到xml的结束返回数字1 END_DOCUMENT ;
//读取到xml的开始标签返回数字2 START_TAG
//读取到xml的结束标签返回数字3 END_TAG
//读取到xml的文本返回数字4 TEXT
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.xmlpull.v1.XmlPullParser;
import android.util.Xml;
import com.xtlh.cn.entity.Book;
public class PullParseService {
public static List<Book> getBooks(InputStream inputStream) throws Exception{
List<Book> books = null;
Book book = null;
XmlPullParser parser = Xml.newPullParser();
parser.setInput(inputStream, "UTF-8");
int event = parser.getEventType();//产生第一个事件
while(event!=XmlPullParser.END_DOCUMENT){
switch(event){
case XmlPullParser.START_DOCUMENT://判断当前事件是否是文档开始事件
books = new ArrayList<Book>();//初始化books集合
break;
case XmlPullParser.START_TAG://判断当前事件是否是标签元素开始事件
if("book".equals(parser.getName())){//判断开始标签元素是否是book
book = new Book();
book.setId(Integer.parseInt(parser.getAttributeValue(0)));//得到book标签的属性值,并设置book的id
}
if(book!=null){
if("name".equals(parser.getName())){//判断开始标签元素是否是name
book.setName(parser.nextText());
}else if("price".equals(parser.getName())){//判断开始标签元素是否是price
book.setPrice(Float.parseFloat(parser.nextText()));
}
}
break;
case XmlPullParser.END_TAG://判断当前事件是否是标签元素结束事件
if("book".equals(parser.getName())){//判断结束标签元素是否是book
books.add(book);//将book添加到books集合
book = null;
}
break;
}
event = parser.next();//进入下一个元素并触发相应事件
}//end while
return books;
}
}