Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > Problem with Xerces-J

Reply
Thread Tools

Problem with Xerces-J

 
 
Louis-Philippe Huberdeau
Guest
Posts: n/a
 
      10-07-2003
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.

--
Louis-Philippe Huberdeau

 
Reply With Quote
 
 
 
 
A. Bolmarcich
Guest
Posts: n/a
 
      10-07-2003
In article <fJHgb.8005$(E-Mail Removed)>, Louis-Philippe Huberdeau wrote:
> 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/java.../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/java.../dom/Node.html

shows that the list of subinterfaces of Node Element and other
non-element types types, such as Attr and CharacterData.
 
Reply With Quote
 
 
 
 
Louis-Philippe Huberdeau
Guest
Posts: n/a
 
      10-08-2003
A. Bolmarcich wrote:
> In article <fJHgb.8005$(E-Mail Removed)>, Louis-Philippe Huberdeau wrote:
>
>>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/java.../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.

>
>
>>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/java.../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.

--
Louis-Philippe Huberdeau

 
Reply With Quote
 
Philippe Poulard
Guest
Posts: n/a
 
      10-08-2003
Louis-Philippe Huberdeau wrote:
> 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.
>
> --
> Louis-Philippe Huberdeau
>


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 &#xA;
-an element node named bar
-a text node with &#xA;
--
Cordialement,

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

 
Reply With Quote
 
Louis-Philippe Huberdeau
Guest
Posts: n/a
 
      10-09-2003
Thanks for the information, that did work. I just never tought a blank
space could be a node.

Philippe Poulard wrote:
> Louis-Philippe Huberdeau wrote:
>
>> 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.
>>
>> --
>> Louis-Philippe Huberdeau
>>

>
> 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 &#xA;
> -an element node named bar
> -a text node with &#xA;


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem problem problem :( Need Help Mike ASP General 2 05-11-2004 08:36 AM



Advertisments