fork(1) download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6. import javax.xml.xpath.*;
  7. import javax.xml.parsers.*;
  8. import org.w3c.dom.*;
  9.  
  10. /* Name of the class has to be "Main" only if the class is public. */
  11. class Ideone
  12. {
  13. public static void main (String[] args) throws java.lang.Exception
  14. {
  15.  
  16. String xml =
  17. "<?xml version='1.0' encoding='UTF-8'?>"+
  18. "<xyz xmlns=\"blah\">"+
  19. "<a>"+
  20. " <b>"+
  21. " <c>X:1 Y:0</c>"+
  22. " <c>X:1 Y:0</c>"+
  23. " <c>X:2 Y:0</c>"+
  24. " </b>"+
  25. " <b>"+
  26. " <c>X:1 Y:0</c>"+
  27. " <c>X:2 Y:0</c>"+
  28. " </b>"+
  29. "</a>"+
  30. "</xyz>";
  31.  
  32. DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  33. factory.setNamespaceAware(true);
  34. DocumentBuilder builder = factory.newDocumentBuilder();
  35. Document doc = builder.parse(new java.io.ByteArrayInputStream(xml.getBytes()));
  36. XPath xpath = XPathFactory.newInstance().newXPath();
  37. XPathExpression xpr = xpath.compile("count(//*[local-name()='xyz']/*[local-name()='a']/*[local-name()='b']/*[local-name()='c'])");
  38. System.out.println(xpr.evaluate(doc, XPathConstants.NUMBER));
  39.  
  40.  
  41.  
  42.  
  43. }
  44. }
Success #stdin #stdout 0.14s 381120KB
stdin
Standard input is empty
stdout
5.0