使用javax.xml解析xml

package org.itat.stax;

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 {

	@Test
	public void test01() {
		XMLInputFactory factory = XMLInputFactory.newInstance();
		InputStream is = null;
		try {
			is = TestStax.class.getClassLoader().getResourceAsStream("books.xml");
			XMLStreamReader reader = factory.createXMLStreamReader(is);
			while(reader.hasNext()) {
				int type = reader.next();
				//鍒ゆ柇鑺傜偣绫诲瀷鏄惁鏄紑濮嬫垨鑰呯粨鏉熸垨鑰呮枃鏈妭鐐�涔嬪悗鏍规嵁鎯呭喌鍙婅繘琛屽鐞�
				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 {
				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("books.xml");
			XMLStreamReader reader = factory.createXMLStreamReader(is);
			while(reader.hasNext()) {
				int type = reader.next();
				if(type==XMLStreamConstants.START_ELEMENT) {
					String name = reader.getName().toString();
					if(name.equals("book")) {
						System.out.println(reader.getAttributeName(0)+":"+reader.getAttributeValue(0));
					}
				}
			}
		} catch (XMLStreamException e) {
			e.printStackTrace();
		} finally {
			try {
				if(is!=null) is.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	@Test
	public void test03() {
		XMLInputFactory factory = XMLInputFactory.newInstance();
		InputStream is = null;
		try {
			is = TestStax.class.getClassLoader().getResourceAsStream("books.xml");
			XMLStreamReader reader = factory.createXMLStreamReader(is);
			while(reader.hasNext()) {
				int type = reader.next();
				
				if(type==XMLStreamConstants.START_ELEMENT) {
					String name = reader.getName().toString();
					if(name.equals("title")) {
						System.out.print(reader.getElementText()+":");
					}
					if(name.equals("price")) {
						System.out.print(reader.getElementText()+"\n");
					}
				}
			}
		} catch (XMLStreamException e) {
			e.printStackTrace();
		} finally {
			try {
				if(is!=null) is.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	@Test
	public void test04() {
		XMLInputFactory factory = XMLInputFactory.newInstance();
		InputStream is = null;
		try {
			is = TestStax.class.getClassLoader().getResourceAsStream("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++;
			}
			System.out.println(num);
		} catch (XMLStreamException e) {
			e.printStackTrace();
		} finally {
			try {
				if(is!=null) is.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	@Test
	public void test05() {
		XMLInputFactory factory = XMLInputFactory.newInstance();
		InputStream is = null;
		try {
			is = TestStax.class.getClassLoader().getResourceAsStream("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++;
			}
			System.out.println(num);
		} catch (XMLStreamException e) {
			e.printStackTrace();
		} finally {
			try {
				if(is!=null) is.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	@Test
	public void test06() {
		InputStream is = null;
		try {
			is = TestStax.class.getClassLoader().getResourceAsStream("books.xml");
			//鍒涘缓鏂囨。澶勭悊瀵硅薄
			DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
			//閫氳繃DocumentBuilder鍒涘缓doc鐨勬枃妗e璞�
			Document doc = db.parse(is);
			//鍒涘缓XPath
			XPath xpath = XPathFactory.newInstance().newXPath();
			//绗竴涓弬鏁板氨鏄痻path,绗簩鍙傛暟灏辨槸鏂囨。
			NodeList list = (NodeList)xpath.evaluate("//book[@category='WEB']", doc,XPathConstants.NODESET);
			for(int i=0;i

 

 

  book.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




 

你可能感兴趣的:(使用javax.xml解析xml)