使用这种方式解析时,需要使用node.getNodeType() == Node.ELEMENT_NODE
判断结点是否为元素结点,否则会输出很多#text
(结点前有空格存在的原因)。
完成的XML解析和生成代码如下:
package test;
import java.io.*;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
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.NodeList;
import org.w3c.dom.Node;
class XMLHelper {
/**
* 解析XML
* @param filename 要解析的XML的文件名
*/
public static void resolveXML(String filename) {
File f = new File(filename);
DocumentBuilderFactory factory = null;
DocumentBuilder db = null;
Element element = null;
try {
//获得DOM解析器工厂
factory = DocumentBuilderFactory.newInstance();
//获取具体的DOM解析器
db = factory.newDocumentBuilder();
//解析一个xml文档,获得Document对象(根节点)
Document document = db.parse(f);
element = document.getDocumentElement();
System.out.println("Root: " + element.getNodeName());
NodeList childNodes = element.getChildNodes();
for (int i = 0; i < childNodes.getLength(); i++) {
Node node = childNodes.item(i);
if (node.getNodeType() == Node.ELEMENT_NODE) {
System.out.println(node.getNodeName());
NodeList nodes = node.getChildNodes();
for (int j = 0; j < nodes.getLength(); j++) {
Node temp = nodes.item(j);
if (temp.getNodeType() == Node.ELEMENT_NODE) {
System.out.println(" " + temp.getNodeName() +
": " + temp.getTextContent());
}
}
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
/**
* 生成XML文件
* @param filename 将生成的XML输出到该文件
*/
public static void CreateXML(String filename) {
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder db = factory.newDocumentBuilder();
Document document = db.newDocument();
Element root = document.createElement("bookstore");
//add book
Element book = document.createElement("book");
book.setAttribute("category", "COOKING");
//book.title
Element title = document.createElement("title");
title.setAttribute("lang", "en");
title.setTextContent("Everyday Italian");
book.appendChild(title);
//book.author
Element author = document.createElement("author");
author.setTextContent("Giada De Laurentiis");
book.appendChild(author);
//book.year
Element year = document.createElement("year");
year.setTextContent("2005");
book.appendChild(year);
//book.price
Element price = document.createElement("price");
price.setTextContent("30.00");
book.appendChild(price);
root.appendChild(book);
//add book
book = document.createElement("book");
book.setAttribute("category", "CHILDREN");
//book.title
title = document.createElement("title");
title.setAttribute("lang", "en");
title.setTextContent("Harry Potter");
book.appendChild(title);
//book.author
author = document.createElement("author");
author.setTextContent("J K. Rowling");
book.appendChild(author);
//book.year
year = document.createElement("year");
year.setTextContent("2005");
book.appendChild(year);
//book.price
price = document.createElement("price");
price.setTextContent("39.99");
book.appendChild(price);
root.appendChild(book);
//add book
book = document.createElement("book");
book.setAttribute("category", "WEB");
//book.title
title = document.createElement("title");
title.setAttribute("lang", "en");
title.setTextContent("Learning XML");
book.appendChild(title);
//book.author
author = document.createElement("author");
author.setTextContent("Erik T. Ray");
book.appendChild(author);
//book.year
year = document.createElement("year");
year.setTextContent("2003");
book.appendChild(year);
//book.price
price = document.createElement("price");
price.setTextContent("39.95");
book.appendChild(price);
root.appendChild(book);
document.appendChild(root);
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
Source xmlSource = new DOMSource(document);
StreamResult outputTarget = new StreamResult(new File(filename));
transformer.transform(xmlSource, outputTarget);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
public class Main {
public static void main(String[] args) throws Exception{
String filename = "E:\\Eclipse Project\\WebTest\\WebContent\\myxml1.xml";
XMLHelper.resolveXML(filename);
filename = "E:\\Eclipse Project\\WebTest\\WebContent\\myxml2.xml";
XMLHelper.CreateXML(filename);
}
}
输入的XML文件内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
<book category="COOKING">
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
<book category="CHILDREN">
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
<book category="WEB">
<title lang="en">Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
</bookstore>
解析函数的输出如下:
Root: bookstore
book
title: Everyday Italian
author: Giada De Laurentiis
year: 2005
price: 30.00
book
title: Harry Potter
author: J K. Rowling
year: 2005
price: 39.99
book
title: Learning XML
author: Erik T. Ray
year: 2003
price: 39.95