0

Is there a method I can grab parent/grandparent nodes? The examples I have found appear to use only xpath statements to select nodes. Some of my failed attempts:

/table/tr/td/b/font[self::text()="Nohup Files"]
/table/descendant::font[self::text()="Nohup Files"]

<table>
<tr>
<td><b><font>Nohup Files</font></b></td>
</tr>
</table>

2
Contributors
4
Replies
5
Views
8 Years
Discussion Span
Last Post by Ezzaral
Featured Replies
  • 1
    Ezzaral 2,714   8 Years Ago

    [ICODE]table[tr/td/b/font='Nohup Files'][/ICODE] seems to work for getting the table element, if that's what you are wanting. I tried it out with the following structure[code]<root> <table> <tr><td><b><font>Not this one</font></b></td></tr> </table> <table> <tr><td><b><font>Nohup Files</font></b></td></tr> </table> <table> <tr><td><b><font>Other</font></b></td></tr> </table> </root>[/code] Read More

1

table[tr/td/b/font='Nohup Files'] seems to work for getting the table element, if that's what you are wanting. I tried it out with the following structure

<root>
    <table>
        <tr><td><b><font>Not this one</font></b></td></tr>
    </table>
    <table>
        <tr><td><b><font>Nohup Files</font></b></td></tr>
    </table>
    <table>
        <tr><td><b><font>Other</font></b></td></tr>
    </table>
</root>
0

Thanks! In my fumblings, I was able to piece together this: //font[self::text()="Nohup Files"]/ancestor::table. Your test xml is exactly what I am working with. What code/classes did you use to test yours?

0

This uses the JDOM packages.

org.jdom.input.SAXBuilder builder = new org.jdom.input.SAXBuilder();
Document doc = builder.build(new File("test.xml"));
XPath xp = XPath.newInstance("table[tr/td/b/font='Nohup Files']");
List<Element> result = xp.selectNodes(doc.getRootElement());
XMLOutputter out = new XMLOutputter();
for(Element e: result){
    System.out.println(out.outputString(e));
}
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.