使用Stax解析XML

发布时间:2017-09-12 15:01:51
使用Stax解析XML 首先是我们用到的位于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")){

企业建站2800元起,携手武汉肥猫科技,做一个有见地的颜值派!更多优惠请戳:武汉网站制作公司哪家好 https://www.feimao666.com