Problem with Xerces-J

  • Thread starter Louis-Philippe Huberdeau
  • Start date
L

Louis-Philippe Huberdeau

The solution to this problem is probably very simple but I can't seem to
find any information about it. I'm trying to parse an XML document with
the Xerces-J (xml.apache.org) DOM implentation. I don't know how common
is the usage of that library but it seemed to be the most documented one
I could find.

The problem is that many functions return child elements as NodeList.
NodeList being a list of Node, I tryed to cast them as Element, to be
able to use getAttribute() on them. When I do so, I get a
ClassCastException. Is there a way to to convert a Node to an Element?

I also have a theorical question, which will probably solve the problem
above... What is the difference between a Node and an Element in XML? In
what cases is a Node not an Element? I'm just trying to figure out why
they seperated both. I'm sure there is a good reason, but I couldn't
find it explicitly and as a student, I don't have enough time to read
the entire specification to find this single information.
 
A

A. Bolmarcich

The solution to this problem is probably very simple but I can't seem to
find any information about it. I'm trying to parse an XML document with
the Xerces-J (xml.apache.org) DOM implentation. I don't know how common
is the usage of that library but it seemed to be the most documented one
I could find.

The problem is that many functions return child elements as NodeList.
NodeList being a list of Node, I tryed to cast them as Element, to be
able to use getAttribute() on them. When I do so, I get a
ClassCastException. Is there a way to to convert a Node to an Element?

A quick look at

http://xml.apache.org/xerces2-j/javadocs/api/org/w3c/dom/NodeList.html

shows that NodeList has a Node item(int) method. Invoking item(0) on a
NodeList should return the first Node of the NodeList.
I also have a theorical question, which will probably solve the problem
above... What is the difference between a Node and an Element in XML? In
what cases is a Node not an Element? I'm just trying to figure out why
they seperated both. I'm sure there is a good reason, but I couldn't
find it explicitly and as a student, I don't have enough time to read
the entire specification to find this single information.

A quick look at

http://xml.apache.org/xerces2-j/javadocs/api/org/w3c/dom/Node.html

shows that the list of subinterfaces of Node Element and other
non-element types types, such as Attr and CharacterData.
 
L

Louis-Philippe Huberdeau

A. Bolmarcich said:
A quick look at

http://xml.apache.org/xerces2-j/javadocs/api/org/w3c/dom/NodeList.html

shows that NodeList has a Node item(int) method. Invoking item(0) on a
NodeList should return the first Node of the NodeList.

That's not the problem, I can get the Node, I just can't use it as an
Element to use the methods I really want to use.
A quick look at

http://xml.apache.org/xerces2-j/javadocs/api/org/w3c/dom/Node.html

shows that the list of subinterfaces of Node Element and other
non-element types types, such as Attr and CharacterData.

I just tought about something related to that. I will test it out
tomorrow and give you feedback about it.

Thank you for your time.
 
P

Philippe Poulard

Louis-Philippe Huberdeau said:
The solution to this problem is probably very simple but I can't seem to
find any information about it. I'm trying to parse an XML document with
the Xerces-J (xml.apache.org) DOM implentation. I don't know how common
is the usage of that library but it seemed to be the most documented one
I could find.

The problem is that many functions return child elements as NodeList.
NodeList being a list of Node, I tryed to cast them as Element, to be
able to use getAttribute() on them. When I do so, I get a
ClassCastException. Is there a way to to convert a Node to an Element?

I also have a theorical question, which will probably solve the problem
above... What is the difference between a Node and an Element in XML? In
what cases is a Node not an Element? I'm just trying to figure out why
they seperated both. I'm sure there is a good reason, but I couldn't
find it explicitly and as a student, I don't have enough time to read
the entire specification to find this single information.

Hi,

In the data model, all object of the document (and it is true for the
Document itself) is a Node.
So, an Element is also a Node.
But a Node may be something else than an Element, for example, a Text
(there is a dozen of types).
So, when you've a NodeList, you should test wich kind of node you got
before casting it :

Node node = list.item(i);
if (node.getNodeType()==Node.ELEMENT_NODE) {
Element el = (Element) node;
// your stuff here
}

Notice that in the following document :
<foo>
<bar>
</foo>
the NodeList obtained from the foo Element will return 3 nodes :
-a text node with

-an element node named bar
-a text node with

--
Cordialement,

///
(. .)
-----ooO--(_)--Ooo-----
| Philippe Poulard |
-----------------------
 
L

Louis-Philippe Huberdeau

Thanks for the information, that did work. I just never tought a blank
space could be a node.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top