Xml解析方式分为两种:
1.dom
document object model 即文档对象模型,它是w3c组织推荐的解析xml的一种方式。
2.sax
simple api for xml 不是官方标准,它是xml 社区事实上的标准,几乎所有的xml解析器都支持它
Jaxp解析
Java api for xml programming 是sun公司的一套操作xml的API. Jaxp很好的支持了dom与sax解析方式。Jaxp开发包是javase的一部分,它是由javax.xml org.w3c.domoirg.xml.saq包及其子包组成
需要类:DocumentBuilderFactory
DocumentBuilder
Document element node 这三个属性是理解xml的关键类,分别指dom, 元素,<node> </node> 开头的内容
- 要xml文件代码
<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
<book catgrory="computer">
<title>java从入门到放弃</title>
<author>王二</author>
<year>1009</year>
<price>43.5</price>
</book>
<book catgrory="story">
<title>mysql从入门到放弃</title>
<author>龙达</author>
<year>2011</year>
<price>23.5</price>
</book>
</bookstore>
- dom代码
当出现了#text ........ 是由于文档间空格换行,需要写逻辑进行处理。
package org.dgw.net;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class demo1 {
public static void main(String[] args) throws ParserConfigurationException,
FileNotFoundException, SAXException, IOException {
// 构建dom解析工厂
DocumentBuilderFactory dom = DocumentBuilderFactory.newInstance();
// 得到解决对象
DocumentBuilder builder = dom.newDocumentBuilder();
// 拿到dom对象
Document document = builder
.parse(new FileInputStream(new File("d://xmldemo.xml")));
// 获取根节点
Element root = document.getDocumentElement();
System.out.println(root.getNodeName());
// 读取database节点NodeList接口提供对节点的有序集合的抽象
NodeList nodeList = root.getElementsByTagName("book");
for (int i = 0; i < nodeList.getLength(); i++) {
// 获取一个节点
Node node = nodeList.item(i);
// 获取该节点所有属性
NamedNodeMap attributes = node.getAttributes();
for (int j = 0; j < attributes.getLength(); j++) {
Node attribute = attributes.item(j);
System.out.println(attribute.getNodeName() + ":"
+ attribute.getNodeValue());
}
// 获取所有子节点数据
NodeList childNodes = node.getChildNodes();
for (int j = 0; j < childNodes.getLength(); j++) {
Node childNode = childNodes.item(j);
if (childNode.getNodeType()==Document.ELEMENT_NODE) {
System.out.println(childNode.getNodeName() + ":"
+ childNode.getTextContent());
}
}
}
}
}
- 生成一个简单文档
类:TransformerFactory Transformer DOMSource StreamResult
package org.xml.tech;
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
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;
public class domcreate {
private static File file;
private final static String path = "D://demo.xml";
public static void main(String[] args) throws ParserConfigurationException,
Exception, TransformerFactoryConfigurationError {
DocumentBuilder builder = DocumentBuilderFactory.newInstance()
.newDocumentBuilder();
Document dom = builder.newDocument();
// 标注xml信息
dom.setXmlVersion("1.0");
dom.setXmlStandalone(true);
// 开始构建
Element root = dom.createElement("root");
root.setAttribute("lang", "zh");
dom.appendChild(root);
// 子节点
Element book = dom.createElement("book");
Element title = dom.createElement("title");
String textContent = "hell wold";
title.setTextContent(textContent);
book.appendChild(title);
root.appendChild(book);
createXml(dom);
}
private static void createXml(Node dom)
throws TransformerFactoryConfigurationError, TransformerException {
Transformer transformer = TransformerFactory.newInstance()
.newTransformer();
DOMSource source = new DOMSource(dom);
file = new File(path);
StreamResult result = new StreamResult(file);
transformer.transform(source, result);
}
}
Sax解析
Sax是基于事件驱动(事件是预定义的,读到什么内容即可触发相关事件)的方式来对xml进行处理。Sax它只能进行读操作。它是读取一行解析一行。大的xml文档采用此读取方式,是不可往复的,不可逆的
相关类: SAXParserFactory SAXParser XMLReader ContentHandler DefaultHandler
package org.xml.tech;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
public class SaxDemo {
public static void main(String[] args) throws ParserConfigurationException,
SAXException, FileNotFoundException, IOException {
SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
XMLReader reader = parser.getXMLReader();
reader.setContentHandler(new DefaultHandler() {
@Override
public void startDocument() throws SAXException {
System.out.println("开始解析");
}
@Override
public void endDocument() throws SAXException {
System.out.println("结束解析");
}
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
System.out.println("开始解析节点[" + qName + "]...");
System.out.println("共有[" + attributes.getLength() + "]个属性");
}
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
// 内容解析
/*
* String string = new String(ch, start, length);
* System.out.println(string);
*/
System.out.println(start);
System.out.println(length);
String string = new String(ch);
System.out.println(string);
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
System.out.println("结束解析XML节点...");
}
});
// 文件源必须写在末尾
reader.parse(
new InputSource(new FileInputStream(new File("D://demo.xml"))));
}
}
Dom4j介绍
Dom4j它是一个优秀操作xml的工具,在很开源的框架中大量使用dom4j, 例如Struts2 hibernate sping框架。dom4j是一个简单的开源库,用于处理XML、 XPath和XSLT,它基于Java平台,使用Java的集合框架,全面集成了DOM,SAX和JAXP,是目前比较流行XML文档解析方法。
要想使用dom4j必须下载dom4j jar包才可以使用。
链接:::http://www.dom4j.org/dom4j-1.6.1/
package org.xml.tech;
import java.io.FileOutputStream;
import java.util.List;
import org.dom4j.Attribute;
import org.dom4j.Document;
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 Dom4jHandler {
public void add() throws Exception {
// 1.创建一个Document
Document document = DocumentHelper.createDocument();
// 2.给Document添加数据
Element root = document.addElement("DataSource");
// 添加注释
root.addComment("这是注释信息");
// 在root根节点下面添加一个子节点
Element database = root.addElement("database");
database.addAttribute("name", "mysql");
database.addAttribute("version", "5.0");
// 添加子节点
database.addElement("driver").setText("com.mysql.jdbc.Driver");
database.addElement("url")
.setText("jdbc:mysql://localhost:3306/myjdbc");
database.addElement("user").setText("root");
database.addElement("password").setText("root");
// 3.将Document写出文件
OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding("utf-8");
// FileOutputStream默认生成的路径在根路径
XMLWriter xw = new XMLWriter(new FileOutputStream("db.xml"), format);
xw.write(document);
xw.close();
}
public void update(String fileName) throws Exception {
// sax解析器
SAXReader saxReader = new SAXReader();
// 读到对象
Document document = saxReader
.read(this.getClass().getResourceAsStream("/" + fileName));
Element root = document.getRootElement();
List<Element> databases_node = root.elements("database");
for (Element database_node : databases_node) {
if (database_node.attributeValue("name")
.equalsIgnoreCase("mysql")) {
System.out
.println("old:" + database_node.attributeValue("name"));
database_node.attribute("name").setText("Oracle");
System.out.println(
"update:" + database_node.attributeValue("name"));
database_node.element("driver").setText("oracel");
database_node.element("url").setText("jdbc");
// 删除password节点
database_node.remove(database_node.element("password"));
// 删除属性
database_node.remove(database_node.attribute("version"));
}
}
OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding("utf-8");
// FileOutputStream默认生成的路径在根路径
XMLWriter xw = new XMLWriter(new FileOutputStream("db2.xml"), format);
xw.write(document);
xw.close();
}
public void read(String fileName) throws Exception {
// sax解析器
SAXReader saxReader = new SAXReader();
// 读到对象
Document document = saxReader
.read(this.getClass().getResourceAsStream("/" + fileName));
Element root = document.getRootElement();
System.out.println("根节点:" + root.getName());
// List<Element> childElements=root.elements();
List<Element> childElements = root.elements("database");
for (Element child : childElements) {
// 获取属性 不知道属性名称时的遍历方法
List<Attribute> attributes = child.attributes();
// for (Attribute attribute : attributes) {
// System.out.println(attribute.getName()+":"+attribute.getValue());
// }
String name = child.attributeValue("name");
// String version = child.attributeValue("version");
String version = child.attribute("version").getValue();
System.out.println(name + ":" + version);
// //获取子节点
// List<Element> childs=child.elements();
// for (Element element : childs) {
// System.out.println(element.getName()+":"+element.getText());
// }
System.out.println(child.elementText("driver"));
System.out.println(child.element("url").getText());
System.out.println(child.elementTextTrim("user"));
System.out.println(child.element("password").getTextTrim());
}
}
public static void main(String[] args) throws Exception {
// new Dom4jHandler().read("data-source.xml");
// new Dom4jHandler().add();
new Dom4jHandler().update("data-source.xml");
}
}
网友评论