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>

Recommended Answers

All 4 Replies

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>

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?

Ooops. Wrong forum.

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));
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.