用xpath解决字符串和xml

前端之家收集整理的这篇文章主要介绍了用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",XPathConstants.NODESET);
			NodeList responseError = (NodeList) xpath.evaluate("//res",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>

猜你在找的XML相关文章