wudhw7283

Untitled

Oct 13th, 2025
41
0
29 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.94 KB | None | 0 0
  1. import javax.xml.parsers.DocumentBuilder;
  2. import javax.xml.parsers.DocumentBuilderFactory;
  3. import javax.xml.xpath.*;
  4. import org.w3c.dom.*;
  5. import java.io.ByteArrayInputStream;
  6. import java.nio.charset.StandardCharsets;
  7.  
  8. // --- 1. Sample XML ---
  9. xml =
  10. "<library>" +
  11.   "<section name='Fiction'>" +
  12.     "<book id='b1' category='fantasy' rating='4.8'>" +
  13.       "<title>Harry Potter and the Philosopher's Stone</title>" +
  14.       "<author nationality='British'>J.K. Rowling</author>" +
  15.       "<price currency='USD'>29.99</price>" +
  16.     "</book>" +
  17.     "<book id='b2' category='fantasy' rating='4.2'>" +
  18.       "<title>The Hobbit</title>" +
  19.       "<author nationality='British'>J.R.R. Tolkien</author>" +
  20.       "<price currency='USD'>25.00</price>" +
  21.     "</book>" +
  22.   "</section>" +
  23.   "<section name='Nonfiction'>" +
  24.     "<book id='b3' category='history' rating='4.7'>" +
  25.       "<title>Sapiens</title>" +
  26.       "<author nationality='Israeli'>Yuval Noah Harari</author>" +
  27.       "<price currency='USD'>35.00</price>" +
  28.     "</book>" +
  29.     "<book id='b4' category='science' rating='4.5'>" +
  30.       "<title>Brief Answers to the Big Questions</title>" +
  31.       "<author nationality='British'>Stephen Hawking</author>" +
  32.       "<price currency='USD'>28.00</price>" +
  33.     "</book>" +
  34.   "</section>" +
  35. "</library>";
  36.  
  37. // --- 2. Parse XML ---
  38. factory = DocumentBuilderFactory.newInstance();
  39. factory.setNamespaceAware(true);
  40. builder = factory.newDocumentBuilder();
  41. doc = builder.parse(new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8)));
  42.  
  43. // --- 3. Setup XPath ---
  44. xpath = XPathFactory.newInstance().newXPath();
  45. results = "";
  46.  
  47. // --- 4. Define helper function (as global) ---
  48. void queryAndAppend(String label, String pattern, String type) throws Exception {
  49.     expr = xpath.compile(pattern);
  50.     results += "[" + label + "] " + pattern + "\n";
  51.  
  52.     if (type.equals("nodes")) {
  53.         nodes = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
  54.         if (nodes.getLength() == 0) {
  55.             results += "(no match)\n\n";
  56.             return;
  57.         }
  58.         for (i = 0; i < nodes.getLength(); i++) {
  59.             val = nodes.item(i).getNodeValue();
  60.             if (val == null) val = nodes.item(i).getTextContent();
  61.             results += "- " + val + "\n";
  62.         }
  63.         results += "\n";
  64.     } else if (type.equals("value")) {
  65.         val = xpath.evaluate(pattern, doc);
  66.         results += "→ " + val + "\n\n";
  67.     }
  68. }
  69.  
  70. // --- 5. Run multiple queries ---
  71. queryAndAppend("A", "//book/title/text()", "nodes");
  72. queryAndAppend("B", "//author[@nationality='British']/text()", "nodes");
  73. queryAndAppend("C", "//book[number(price) > 28]/title/text()", "nodes");
  74. queryAndAppend("D", "//book[number(@rating) >= 4.5]/title/text()", "nodes");
  75. queryAndAppend("E", "count(//book)", "value");
  76. queryAndAppend("F", "//section/@name", "nodes");
  77.  
  78. // --- 6. Return the full report ---
  79. tasker.log("XPath Results:\n" + results);
  80. return results.trim();
  81.  
Advertisement
Add Comment
Please, Sign In to add comment