11

hi all I'm trying to parse a DOM tree using Neko/Xerces in Java.

NodeList divs = this.doc.getElementsByTagName("DIV");
for(int i=0; i < divs.getLength(); i++) {
    NodeList images = divs.item(i).parentNode().getElementsByTagName("IMG");
    // operate on these
}

is what I'd ideally like to do. It seems I can only call getElementsByTagName on the document itself? Am I doing something wrong? Should I be able to call that on a Node element?

I can see from the docs it's not there: http://xerces.apache.org/xerces-j/apiDocs/org/w3c/dom/Node.html so maybe I need to do it another way?

thanks!

Guido
  • 46,642
  • 28
  • 120
  • 174
James
  • 15,085
  • 25
  • 83
  • 120

2 Answers2

9

A NodeList only returns Nodes and getElementsByTagName is only available on an Element node You therefore need to cast your Node to an element, here's an example below.

final NodeList images = ((Element)divs.item(i).getParentNode()).getElementsByTagName("IMG");

However be careful with this as it assumes that getParentNode() always returns an Element

This would be safer, but a lot more verbose

final Node n = divs.item(i).getParentNode();

if(n instanceof Element) {
    final Element e = (Element)n;
    e.getElementsByTagName("IMG");
}
reevesy
  • 3,452
  • 1
  • 26
  • 23
0

Yeah, that's weird. Python's xml.dom.minidom has a Node.getElementsByTagName. Maybe it's not part of the standard. Instead, you could iterate an inner loop over divs.item(i).parentNode().getChildNodes().

Aaron Altman
  • 1,705
  • 1
  • 14
  • 22