java解析xml的几种方式(转)

各种方法都用过。现在总结一下。 经常记不住,要找资料。现在总结一下。

xpath 语法 http://www.w3school.com.cn/xpath/xpath_syntax.asp

xml 文件如下:

 
<?xml version="1.0" encoding="ISO-8859-1"?>
<bookstore>
<book category="COOKING">
  <title lang="en">Everyday Italian</title>
  <author>Giada De Laurentiis</author>
  <year>2005</year>
  <price>30.00</price>
  <aa>
  	<bb>
  		<cc>ccccc</cc>
  	</bb>
  </aa>
</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>



[
package sort;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
public class D2 {
	/**
	 * 直接使用DOM解析
	 * @param args
	 * @throws Exception
	 */
	public static void main(String[] args) throws Exception{
		
		DocumentBuilder sb =  DocumentBuilderFactory.newInstance().newDocumentBuilder();
		
		Document root = sb.parse(D2.class.getClassLoader().getResourceAsStream("NewFile.xml"));
		
		System.out.println(root.getChildNodes().item(0).getNodeName());
		
	}
}


package sort;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl;

public class D {

	/**
	 * 使用SAX解析
	 * @param args
	 * @throws Exception
	 */
	public static void main(String[] args) throws Exception {
		
		SAXParserFactory factory = SAXParserFactoryImpl.newInstance();
		SAXParser parser = factory.newSAXParser() ;
		parser.parse(D.class.getClassLoader().getResourceAsStream("NewFile.xml"), 
				new DefaultHandler(){

					@Override
					public void characters(char[] ch, int start, int length)
							throws SAXException {
						System.out.println("characters");
					}

					@Override
					public void endDocument() throws SAXException {
						// TODO Auto-generated method stub
						System.out.println("endDocument");
					}

					@Override
					public void endElement(String uri, String localName,
							String qName) throws SAXException {
						// TODO Auto-generated method stub
						System.out.println("endElement");
					}

					@Override
					public void startDocument() throws SAXException {
						// TODO Auto-generated method stub
						System.out.println("startDocument");
					}

					@Override
					public void startElement(String uri, String localName,
							String qName, Attributes attributes)
							throws SAXException {
						// TODO Auto-generated method stub
						System.out.println("startElement");
					}
			
		}) ;
		
		
	}
	
}


package sort;

import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamReader;

public class D3 {

	/**
	 * 使用XMLStream解析
	 * @param args
	 * @throws Exception
	 */
	public static void main(String[] args) throws Exception {

		XMLInputFactory xmlFactor = XMLInputFactory.newFactory();

		XMLStreamReader reader = 
			xmlFactor.createXMLStreamReader(D3.class.getClassLoader().getResourceAsStream("NewFile.xml"));
		while(reader.hasNext()){
			int point = reader.next() ;
			switch(point){
			case XMLStreamReader.START_ELEMENT :
				System.out.println("start_element");
			case XMLStreamReader.END_ELEMENT :
				// do something...
			}

		}

	}

}

 

你可能感兴趣的:(java,xml)