用xpath解决字符串和xml

前端之家收集整理的这篇文章主要介绍了用xpath解决字符串和xml前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
  1. package xpath;
  2.  
  3. import java.io.ByteArrayInputStream;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6.  
  7. import javax.xml.parsers.DocumentBuilder;
  8. import javax.xml.parsers.DocumentBuilderFactory;
  9. import javax.xml.parsers.ParserConfigurationException;
  10. import javax.xml.xpath.XPath;
  11. import javax.xml.xpath.XPathConstants;
  12. import javax.xml.xpath.XPathExpressionException;
  13. import javax.xml.xpath.XPathFactory;
  14.  
  15. import org.w3c.dom.Document;
  16. import org.w3c.dom.Element;
  17. import org.w3c.dom.NodeList;
  18. import org.xml.sax.SAXException;
  19.  
  20. public class XPathTest {
  21.  
  22. /**
  23. * @param args
  24. */
  25. public static void main(String[] args) {
  26. // test();
  27. soapTest();
  28. }
  29.  
  30. private static void test() {
  31. // xml文档的形式读入
  32. InputStream is = XPathTest.class.getClassLoader().getResourceAsStream(
  33. "book.xml");
  34.  
  35. try {
  36. DocumentBuilder db = DocumentBuilderFactory.newInstance()
  37. .newDocumentBuilder();
  38. Document doc = db.parse(is);
  39. XPath xpath = XPathFactory.newInstance().newXPath();
  40. NodeList nodeList = (NodeList) xpath.evaluate(
  41. "//book[@category=\"WEB\"]",doc,XPathConstants.NODESET);
  42. for (int i = 0; i < nodeList.getLength(); i++) {
  43. Element e = (Element) nodeList.item(i);
  44. NodeList subList = e.getElementsByTagName("author");
  45. for (int j = 0; j < subList.getLength(); j++) {
  46. System.out.println(subList.item(j).getTextContent());
  47. }
  48. // String text = (String)nodeList.item(i).getTextContent();
  49. // System.out.println(text);
  50. }
  51. } catch (SAXException e) {
  52. // TODO Auto-generated catch block
  53. e.printStackTrace();
  54. } catch (IOException e) {
  55. // TODO Auto-generated catch block
  56. e.printStackTrace();
  57. } catch (ParserConfigurationException e) {
  58. // TODO Auto-generated catch block
  59. e.printStackTrace();
  60. } catch (XPathExpressionException e) {
  61. // TODO Auto-generated catch block
  62. e.printStackTrace();
  63. }
  64.  
  65. }
  66.  
  67. private static void soapTest() {
  68. try {
  69. DocumentBuilder db = DocumentBuilderFactory.newInstance()
  70. .newDocumentBuilder();
  71. // 字符串的形式读入
  72. String str = "<req><res error=\"0\" affected=\"1\"></res><rset><row><rv>DayPass,DayPassPlus</rv></row></rset></req>";
  73. InputStream in = new ByteArrayInputStream(str.getBytes());
  74. Document doc = db.parse(in);
  75. XPath xpath = XPathFactory.newInstance().newXPath();
  76. NodeList responseTextList = (NodeList) xpath.evaluate("//rv",XPathConstants.NODESET);
  77. NodeList responseError = (NodeList) xpath.evaluate("//res",XPathConstants.NODESET);
  78. String error = responseError.item(0).getAttributes()
  79. .getNamedItem("error").getTextContent();
  80. String responseText = responseTextList.item(0).getTextContent();
  81. System.out.println("error: " + error + ",text: " + responseText);
  82. // System.out.println(list.item(0).getTextContent());
  83. /*
  84. * for(int i = 0; i < list.getLength(); i++){ Element e =
  85. * (Element)list.item(i); String re = e.getNodeValue();
  86. * System.out.println(re); }
  87. */
  88. } catch (ParserConfigurationException e) {
  89. // TODO Auto-generated catch block
  90. e.printStackTrace();
  91. } catch (SAXException e) {
  92. // TODO Auto-generated catch block
  93. e.printStackTrace();
  94. } catch (IOException e) {
  95. // TODO Auto-generated catch block
  96. e.printStackTrace();
  97. } catch (XPathExpressionException e) {
  98. // TODO Auto-generated catch block
  99. e.printStackTrace();
  100. }
  101. }
  102.  
  103. }

  1. <?xml version="1.0" encoding="ISO-8859-1"?>
  2.  
  3.  
  4. <bookstore>
  5.  
  6.  
  7. <book category="COOKING">
  8. <title lang="en">Everyday Italian</title>
  9. <author>Giada De Laurentiis</author>
  10. <year>2005</year>
  11. <price>30.00</price>
  12. </book>
  13.  
  14.  
  15. <book category="CHILDREN">
  16. <title lang="en">Harry Potter</title>
  17. <author>J K. Rowling</author>
  18. <year>2005</year>
  19. <price>29.99</price>
  20. </book>
  21.  
  22.  
  23. <book category="WEB">
  24. <title lang="en">XQuery Kick Start</title>
  25. <author>James McGovern</author>
  26. <author>Per Bothner</author>
  27. <author>Kurt Cagle</author>
  28. <author>James Linn</author>
  29. <author>Vaidyanathan Nagarajan</author>
  30. <year>2003</year>
  31. <price>49.99</price>
  32. </book>
  33.  
  34.  
  35. <book category="WEB">
  36. <title lang="en">Learning XML</title>
  37. <author>Erik T. Ray</author>
  38. <year>2003</year>
  39. <price>39.95</price>
  40. </book>
  41.  
  42.  
  43. </bookstore>

猜你在找的XML相关文章