Can't call getElementsByTagName on a node in Xerces or Neko?
Asked Answered
V

2

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!

Vagrancy answered 22/7, 2010 at 23:1 Comment(0)
C
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");
}
Cremona answered 23/6, 2011 at 14:4 Comment(0)
C
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().

Chaplain answered 23/7, 2010 at 3:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.