Note that there are some explanatory texts on larger screens.

plurals
  1. POXpath expression won't select anything
    primarykey
    data
    text
    <p>HI there, I have an xml that is dynamically generated, from which I want to extract the information..I was using Xpath expressions but found out my xpath expression is not working properly it doesn't select anything.....here's my Java function which executes the xpath expression:</p> <pre><code>public void TraverseClass(NavigationTree parent,String name) { xPath = XPathFactory.newInstance().newXPath(); XPathExpression expr; try { expr = xPath.compile("Class[@name=\""+name+"\"]/Class"); Object result = expr.evaluate(document, XPathConstants.NODESET); NodeList nodes = (NodeList) result; JOptionPane.showMessageDialog(null,nodes.getLength()); for(int i=0;i&lt;=nodes.getLength()-1;i++) { Node first=nodes.item(i); Element node=(Element)first; JOptionPane.showMessageDialog(null,"Found Classes inside "+name+" " +node.getAttribute("name")); parent.foundClass(node.getAttribute("name")); //Interfaces[i]=node.getAttribute("name"); } expr = xPath.compile("Class[@name=\""+name+"\"]/Interfaces"); result = expr.evaluate(document, XPathConstants.NODESET); nodes = (NodeList) result; JOptionPane.showMessageDialog(null,nodes.getLength()); for(int i=0;i&lt;=nodes.getLength()-1;i++) { Node first=nodes.item(i); Element node=(Element)first; JOptionPane.showMessageDialog(null,"Found Interfaces inside "+name+" "+node.getAttribute("name")); parent.foundInterface(node.getAttribute("name")); //Interfaces[i]=node.getAttribute("name"); } expr = xPath.compile("Class[@name=\""+name+"\"]/Method"); result = expr.evaluate(document, XPathConstants.NODESET); nodes = (NodeList) result; JOptionPane.showMessageDialog(null,nodes.getLength()); for(int i=0;i&lt;=nodes.getLength()-1;i++) { Node first=nodes.item(i); Element node=(Element)first; JOptionPane.showMessageDialog(null,"Found Methods inside "+name+" "+node.getAttribute("name")); parent.foundMethod(node.getAttribute("name")); //Interfaces[i]=node.getAttribute("name"); } } catch (XPathExpressionException e) { JOptionPane.showMessageDialog(null,"error: "+e.toString()); e.printStackTrace(); } JOptionPane.showMessageDialog(null,"going one step back in Class "+name); parent.goOneStepBack(); } </code></pre> <p>Now the xml file I am trying to parse is</p> <pre><code>&lt;?xml version="1.0"?&gt; &lt;program&gt; &lt;Interface modifier="public" name="interface1"&gt; &lt;ParentInterface&gt; i1&lt;/ParentInterface&gt; &lt;ParentInterface&gt; i2&lt;/ParentInterface&gt; &lt;ParentInterface&gt; i3&lt;/ParentInterface&gt; &lt;Field modifier="" type="int" name="a,b,c"&gt; &lt;/Field&gt; &lt;Interface modifier="public" name="one"&gt; &lt;ParentInterface&gt; two&lt;/ParentInterface&gt; &lt;/Interface&gt; &lt;Class modifier="public" name="three"&gt; &lt;Parent&gt;four&lt;/Parent&gt; &lt;/Class&gt; &lt;Method modifier="" type="void" name="foo1"&gt; &lt;FormalParameter &gt; &lt;/FormalParameter&gt; &lt;/Method&gt; &lt;/Interface&gt; &lt;Class modifier="" name="FrontPAge"&gt; &lt;Parent&gt;JFrame&lt;/Parent&gt; &lt;Field modifier="public" type="int" name="a,b,c"&gt; &lt;/Field&gt; &lt;Field modifier="" type="int" name="arr[]"&gt; &lt;/Field&gt; &lt;Field modifier="" type="int" name="arr2[]"&gt; &lt;/Field&gt; &lt;Field modifier="" type="int" name="num[]"&gt; &lt;/Field&gt; &lt;Field modifier="" type="FrontPAge" name="fp"&gt; &lt;/Field&gt; &lt;Constructor modifier="public" name="FRontPage"&gt; &lt;FormalParameter &gt; &lt;/FormalParameter&gt; &lt;Statement&gt; &lt;![CDATA[System.out.println("It is a constructor")]]&gt;&lt;/Statement&gt; &lt;/Constructor&gt; &lt;Method modifier="publicstatic" type="void" name="main"&gt; &lt;FormalParameter modifier="" type="String[]" var_name="args" &gt; &lt;/FormalParameter&gt; &lt;Field modifier="" type="int" name="x,y,z"&gt; &lt;/Field&gt; &lt;Field modifier="" type="int" name="sum[]"&gt; &lt;/Field&gt; &lt;For&gt; &lt;Itr_Var type="int"&gt;x&lt;/Itr_Var&gt; &lt;Collection&gt;&lt;![CDATA[num]]&gt;&lt;/Collection&gt; &lt;Statement&gt; &lt;![CDATA[sum+=x]]&gt;&lt;/Statement&gt; &lt;/For&gt; &lt;Interface modifier="public" name="qqq"&gt; &lt;ParentInterface&gt; www&lt;/ParentInterface&gt; &lt;/Interface&gt; &lt;Statement&gt; &lt;![CDATA[System.out.println("Runnable is gonna start")]]&gt;&lt;/Statement&gt; &lt;Class name="Runnable"&gt; &lt;Method modifier="public" type="void" name="run"&gt; &lt;FormalParameter &gt; &lt;/FormalParameter&gt; &lt;Statement&gt; &lt;![CDATA[System.out.println("Hello")]]&gt;&lt;/Statement&gt; &lt;/Method&gt; &lt;/Class&gt; &lt;Statement&gt; &lt;![CDATA[EventQueue.invokeLater(newRunnable(){publicvoidrun() {System.out.println("Hello");}})]]&gt;&lt;/Statement&gt; &lt;Statement&gt; &lt;![CDATA[System.out.println("Runnable Ends Here")]]&gt;&lt;/Statement&gt; &lt;Statement&gt; &lt;![CDATA[c=b+d]]&gt;&lt;/Statement&gt; &lt;Statement&gt; &lt;![CDATA[a=b+c]]&gt;&lt;/Statement&gt; &lt;/Method&gt; &lt;Class modifier="" name="nested"&gt; &lt;/Class&gt; &lt;Interface modifier="public" name="xxx"&gt; &lt;ParentInterface&gt; yyy&lt;/ParentInterface&gt; &lt;/Interface&gt; &lt;/Class&gt; &lt;/program&gt; </code></pre> <p>Information: The name parameter of java function contains the name of one highest level classes,i.e descendants of root element which may futrther contain other classes/interfaces/methods...</p> <p>Question: My problem is the expression I am using <code>Class[@name=&lt;ClassName&gt;]/Class</code> should select all the classes from the xml,exclude everything other than select the class with the name and then select all the child classes of that class...same thing goes with the interfaces and methods...but it doesn't work at all!!!!....none of the expressions match anything....</p> <p>I can't rely on absolute paths much because xml is generated dynamically from source codes and thus content of xml except for the root element too are subjected to change</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload