stax解析xml文档的6种方式

原文链接:http://blog.csdn.net/u011593278/article/details/9745271
stax解析xml文档的方式:
  1. 基于光标的查询;
  2. 基于迭代模型的查找;
  3. 基于过滤器的查找;
  4. 基于xpath的查找;
  5. 使用 XMLStreamWriter 创建 xml 文档:
  6. 通过 Transformer 更新节点信息;
package com.zp.ws.test03;

import java.io.IOException;
import java.io.InputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
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.Result;
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 javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.junit.Test;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class TestStax {
	
	/**
	 * 基于光标模型:
	 * 
	 * 获取xml文件的元素和文本内容;
	 */
	@Test
	public void test01() {
		//实例化一个xml输入流工厂,创建一个xml文件读取器;
		XMLInputFactory factory = XMLInputFactory.newInstance();
		InputStream is = null;
		try {
			//获取xml文件的的输入流;
			is = TestStax.class.getClassLoader().getResourceAsStream("com/zp/ws/test03/books.xml");
			//创建一个XMLStreamReader,读取xml文件,获取相应内容;
			XMLStreamReader reader = factory.createXMLStreamReader(is);
			while(reader.hasNext()) {
				int type = reader.next();
				/**
				 * 判断节点类型是否是开始或者结束或者文本节点,之后根据情况及进行处理:
				 * 
				 * START_ELEMENT:开始元素;
				 * CHARACTERS:文本;
				 * END_ELEMENT:结束元素;
				 * 
				 * reader.getName()得先判断是什么节点,再输出name,否则会报错,因为文本节点没有name;
				 */
				if(type==XMLStreamConstants.START_ELEMENT) {
					System.out.println(reader.getName());
				} else if(type==XMLStreamConstants.CHARACTERS) {
					System.out.println(reader.getText().trim());
				} else if(type==XMLStreamConstants.END_ELEMENT) {
					System.out.println("/"+reader.getName());
				}
			}
		} catch (XMLStreamException e) {
			e.printStackTrace();
		} finally {
			try {
				//关闭读取xml文件的输入流;
				if(is!=null) is.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * 基于光标模型:
	 * 
	 * 获取指定元素名的属性名和属性值;
	 */
	@Test
	public void test02() {
		XMLInputFactory factory = XMLInputFactory.newInstance();
		InputStream is = null;
		try {
			is = TestStax.class.getClassLoader().getResourceAsStream("com/zp/ws/test03/books.xml");
			XMLStreamReader reader = factory.createXMLStreamReader(is);
			while(reader.hasNext()) {
				int type = reader.next();
				if(type==XMLStreamConstants.START_ELEMENT) {
					String name = reader.getName().toString();
					//获取元素名称为book的第一个属性名和属性值;
					if(name.equals("book")) {
						System.out.println(reader.getAttributeName(0)+":"+reader.getAttributeValue(0));
					}
				}
			}
		} catch (XMLStreamException e) {
			e.printStackTrace();
		} finally {
			try {
				//关闭读取xml文件的输入流;
				if(is!=null) is.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * 基于光标模型:
	 * 
	 * 获取xml文件指定元素的文本内容(元素的值)
	 */
	@Test
	public void test03() {
		XMLInputFactory factory = XMLInputFactory.newInstance();
		InputStream is = null;
		try {
			is = TestStax.class.getClassLoader().getResourceAsStream("com/zp/ws/test03/books.xml");
			XMLStreamReader reader = factory.createXMLStreamReader(is);
			while(reader.hasNext()) {
				int type = reader.next();
				
				if(type==XMLStreamConstants.START_ELEMENT) {
					String name = reader.getName().toString();
					//获取名为"title"的元素的文本内容(元素的值)
					if(name.equals("title")) {
						System.out.print(reader.getElementText()+":");
					}
					//获取名为"price"的元素的文本内容(元素的值)
					if(name.equals("price")) {
						System.out.print(reader.getElementText()+"\n");
					}
				}
			}
		} catch (XMLStreamException e) {
			e.printStackTrace();
		} finally {
			try {
				//关闭读取xml文件的输入流;
				if(is!=null) is.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * 基于迭代模型:
	 * 
	 * 获取xml文件指定元素的文本内容(元素的值)
	 * 
	 * 迭代次数:本次测试时85;
	 * 
	 * 根据迭代次数和过滤模型相比,过滤模型的远高于迭代模型的;
	 */
	@Test
	public void test04() {
		XMLInputFactory factory = XMLInputFactory.newInstance();
		InputStream is = null;
		try {
			is = TestStax.class.getClassLoader().getResourceAsStream("com/zp/ws/test03/books.xml");
			//基于迭代模型的操作方式
			XMLEventReader reader = factory.createXMLEventReader(is);
			int num = 0;
			while(reader.hasNext()) {
				//通过XMLEvent来获取是否是某种节点类型
				XMLEvent event = reader.nextEvent();
				if(event.isStartElement()) {
					//通过event.asxxx转换节点类型;
					String name = event.asStartElement().getName().toString();
					if(name.equals("title")) {
						System.out.print(reader.getElementText()+":");
					}
					if(name.equals("price")) {
						System.out.print(reader.getElementText()+"\n");
					}
				}
				num++;
			}
			//返回迭代次数是:85;
			System.out.println(num);
		} catch (XMLStreamException e) {
			e.printStackTrace();
		} finally {
			try {
				//关闭读取xml文件的输入流;
				if(is!=null) is.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * 基于过滤模型:
	 * 
	 * 获取xml文件指定元素的文本内容(元素的值)
	 * 
	 * 加过滤器后的迭代次数:本次测试结果:8;
	 * 
	 * 所以可以得到的结果是:使用过滤模型的效率远高于迭代模型;
	 */
	@Test
	public void test05() {
		XMLInputFactory factory = XMLInputFactory.newInstance();
		InputStream is = null;
		try {
			is = TestStax.class.getClassLoader().getResourceAsStream("com/zp/ws/test03/books.xml");
			//基于Filter的过滤方式,可以有效的过滤掉不用进行操作的节点,效率会高一些
			XMLEventReader reader = factory.createFilteredReader(factory.createXMLEventReader(is),
					new EventFilter() {
						@Override
						public boolean accept(XMLEvent event) {
							//返回true表示会显示,返回false表示不显示
							if(event.isStartElement()) {
								String name = event.asStartElement().getName().toString();
								if(name.equals("title")||name.equals("price"))
									return true;
							}
							return false;
						}
					});
			int num = 0;
			while(reader.hasNext()) {
				//通过XMLEvent来获取是否是某种节点类型
				XMLEvent event = reader.nextEvent();
				if(event.isStartElement()) {
					//通过event.asxxx转换节点
					String name = event.asStartElement().getName().toString();
					if(name.equals("title")) {
						System.out.print(reader.getElementText()+":");
					}
					if(name.equals("price")) {
						System.out.print(reader.getElementText()+"\n");
					}
				}
				num++;
			}
			//返回迭代次数:8;
			System.out.println(num);
		} catch (XMLStreamException e) {
			e.printStackTrace();
		} finally {
			try {
				//关闭读取xml文件的输入流;
				if(is!=null) is.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * 基于XPath :XPath 是一门在 XML 文档中查找信息的语言;
	 */
	@Test
	public void test06() {
		InputStream is = null;
		try {
			is = TestStax.class.getClassLoader().getResourceAsStream("com/zp/ws/test03/books.xml");
			//创建文档处理对象
			DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
			//通过DocumentBuilder创建doc的文档对象
			Document doc = db.parse(is);
			//创建XPath
			XPath xpath = XPathFactory.newInstance().newXPath();
			//第一个参数就是xpath,第二参数就是文档
			NodeList list = (NodeList)xpath.evaluate("//book[@category='WEB']", doc,XPathConstants.NODESET);
			for(int i=0;i

books.xml文件代码:





  Everyday Italian
  Giada De Laurentiis
  2005
  30.00



  Harry Potter
  J K. Rowling
  2005
  29.99



  XQuery Kick Start
  James McGovern
  Per Bothner
  Kurt Cagle
  James Linn
  Vaidyanathan Nagarajan
  2003
  49.99



  Learning XML
  Erik T. Ray
  2003
  39.95





你可能感兴趣的:(WebService)