Problem with Xerces-J

Discussion in 'XML' started by Louis-Philippe Huberdeau, Oct 8, 2003.

  1. 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
    Louis-Philippe Huberdeau, Oct 8, 2003
    #1
    1. Advertising

  2. In article <fJHgb.8005$>, 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/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.
    A. Bolmarcich, Oct 8, 2003
    #2
    1. Advertising

  3. A. Bolmarcich wrote:
    > In article <fJHgb.8005$>, 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/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.

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


    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
    Louis-Philippe Huberdeau, Oct 8, 2003
    #3
  4. 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

    -an element node named bar
    -a text node with

    --
    Cordialement,

    ///
    (. .)
    -----ooO--(_)--Ooo-----
    | Philippe Poulard |
    -----------------------
    Philippe Poulard, Oct 8, 2003
    #4
  5. 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

    > -an element node named bar
    > -a text node with
    Louis-Philippe Huberdeau, Oct 9, 2003
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Roy Benjamin
    Replies:
    3
    Views:
    532
    Mike Schilling
    Jul 23, 2003
  2. Grzegorz Wrazen

    Xerces problem with jar file

    Grzegorz Wrazen, Aug 30, 2004, in forum: Java
    Replies:
    1
    Views:
    338
    Aria Kokoschka
    Sep 1, 2004
  3. Soeren
    Replies:
    2
    Views:
    352
    Waxolunist
    Sep 22, 2004
  4. Mark F
    Replies:
    0
    Views:
    449
    Mark F
    Mar 7, 2005
  5. cvissy
    Replies:
    0
    Views:
    603
    cvissy
    Nov 16, 2004
Loading...

Share This Page