用xpath解決字符串和xml

package xpath;

import java.io.ByteArrayInputStream;
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.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

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

public class XPathTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// test();
		soapTest();
	}

	private static void test() {
		// xml文檔的形式讀入
		InputStream is = XPathTest.class.getClassLoader().getResourceAsStream(
				"book.xml");

		try {
			DocumentBuilder db = DocumentBuilderFactory.newInstance()
					.newDocumentBuilder();
			Document doc = db.parse(is);
			XPath xpath = XPathFactory.newInstance().newXPath();
			NodeList nodeList = (NodeList) xpath.evaluate(
					"//book[@category=\"WEB\"]", doc, XPathConstants.NODESET);
			for (int i = 0; i < nodeList.getLength(); i++) {
				Element e = (Element) nodeList.item(i);
				NodeList subList = e.getElementsByTagName("author");
				for (int j = 0; j < subList.getLength(); j++) {
					System.out.println(subList.item(j).getTextContent());
				}
				// String text = (String)nodeList.item(i).getTextContent();
				// System.out.println(text);
			}
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (XPathExpressionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	private static void soapTest() {
		try {
			DocumentBuilder db = DocumentBuilderFactory.newInstance()
					.newDocumentBuilder();
			// 字符串的形式讀入
			String str = "<req><res error=\"0\" affected=\"1\"></res><rset><row><rv>DayPass,DayPassPlus</rv></row></rset></req>";
			InputStream in = new ByteArrayInputStream(str.getBytes());
			Document doc = db.parse(in);
			XPath xpath = XPathFactory.newInstance().newXPath();
			NodeList responseTextList = (NodeList) xpath.evaluate("//rv", doc,
					XPathConstants.NODESET);
			NodeList responseError = (NodeList) xpath.evaluate("//res", doc,
					XPathConstants.NODESET);
			String error = responseError.item(0).getAttributes()
					.getNamedItem("error").getTextContent();
			String responseText = responseTextList.item(0).getTextContent();
			System.out.println("error: " + error + ", text: " + responseText);
			// System.out.println(list.item(0).getTextContent());
			/*
			 * for(int i = 0; i < list.getLength(); i++){ Element e =
			 * (Element)list.item(i); String re = e.getNodeValue();
			 * System.out.println(re); }
			 */
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (XPathExpressionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}

<?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>
</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>


發佈了37 篇原創文章 · 獲贊 3 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章