首先是我们用到的位于src下面的books.xml
[html]
<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
<book category="COOKING" aaaaaa="111111">
<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">XQuery Kick Start</title>
<author>James McGovern</author>
<author>Per Bothner</author>
<author>Kurt Cagle</author>
<author>James Linn</author>
<author>Vaidyanathan Nagarajan</author>
<year>2003</year>
<price>49.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>
下面是Stax用法
[java]
package com.jadyer.stax;
import java.io.InputStream;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.stream.EventFilter;
import javax.xml.stream.FactoryConfigurationError;
import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import javax.xml.stream.XMLStreamWriter;
import javax.xml.stream.events.XMLEvent;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
/**
* 使用Stax解析XML
* @see Stax(Streaming API for XML)
* @create Mar 17, 2013 5:30:31 PM
* @author 玄玉<http://blog.csdn/net/jadyer>
*/
public class TestStax {
public static void main(String[] args) throws Exception {
cursorParseXML();
iteratorParseXML();
filterParseXML();
xpathParseXML();
xmlStreamWriterXML();
transformerModifyXML();
}
/**
* 基于光标的方式解析XML
*/
private static void cursorParseXML() throws Exception {
XMLInputFactory factory = XMLInputFactory.newInstance();
InputStream is = TestStax.class.getClassLoader().getResourceAsStream("books.xml");
XMLStreamReader reader = factory.createXMLStreamReader(is);
while(reader.hasNext()) {
int type = reader.next();
//处理不同的节点类型
//处理开始节点,如<bookstore>,<book>
if(type == XMLStreamConstants.START_ELEMENT) {
String name = reader.getName().toString();
System.out.println("<" + name + ">");
//打印<book category="COOKING" aaaaaa="111111">节点中的第二个属性值aaaaaa
if(name.equals("book")){
System.out.println("节点属性值为" + reader.getAttributeName(1) + "=" + reader.getAttributeValue(1));
}
if(name.equals("title")) {
System.out.println("书籍[" + reader.getElementText() + "]");
}
if(name.equals("price")) {
System.out.println("的价格为[" + reader.getElementText() + "]");
}
//打印文本节点的内容,注意各xml标签中的空白部分也属于文本节点..并且文本节点是没有name的,所以不能getName
} else if(type == XMLStreamConstants.CHARACTERS) {
System.out.println(&qu
补充:Web开发 , 其他 ,