Java Sun XML parser weird behaviour

Discussion in 'Java' started by Dennis, Feb 2, 2006.

  1. Dennis

    Dennis Guest

    <?xml version="1.0" encoding="utf-8"?>
    <!DOCTYPE database [
    <!ELEMENT database (item)*>
    <!ELEMENT item (name,sequence)>
    <!ELEMENT name (#PCDATA)>
    <!ELEMENT sequence (#PCDATA)>
    ]>
    <database>
    <item>
    <name>YBL014c</name>
    <sequence>ATGAGTG</sequence>
    </item>
    <item>
    <name>YBL025w</name>
    <sequence>ATGCTGA</sequence>
    </item>
    </database>

    Here's the example I'm working with. When I call
    getElementsByName("item"), it returns 2 items as expected. Then when I
    call getChildNodes, it returns a NodeList of length 5 for some reason,
    even though there are only two child nodes per item? Two of them are
    the "name" and "sequence" nodes, and 3 of them are "#text". Plus,
    getting to the node values in the "name" and "sequence" nodes is not
    completely intuitive. I can get to them, but the method calls seem
    weird. (i.e. code below for tempName and tempSeq). If there were more
    than two nodes in each "item", how would I get to it, since in this
    example I'm coming at the values from opposite sides?

    Oh, I'm using the DocumentBuilderFactory -> DocumentBuilder -> Document
    - XML Parser from the javax.xml.parsers package.

    In my code: nl is a NodeList from getElementsByTagName("item");

    ....
    Node temp = nl.item(i);

    NodeList tl = temp.getChildNodes();
    Node tn = nl.item(i);

    tempName =
    tn.getFirstChild().getNextSibling().getFirstChild().getNodeValue();

    tempSeq =
    tn.getLastChild().getPreviousSibling().getFirstChild().getNodeValue();
    ....


    Dennis
     
    Dennis, Feb 2, 2006
    #1
    1. Advertising

  2. Dennis wrote:


    > <database>
    > <item>
    > <name>YBL014c</name>
    > <sequence>ATGAGTG</sequence>
    > </item>
    > <item>
    > <name>YBL025w</name>
    > <sequence>ATGCTGA</sequence>
    > </item>
    > </database>
    >
    > Here's the example I'm working with. When I call
    > getElementsByName("item"), it returns 2 items as expected. Then when I
    > call getChildNodes, it returns a NodeList of length 5 for some reason,
    > even though there are only two child nodes per item? Two of them are
    > the "name" and "sequence" nodes, and 3 of them are "#text".


    There are other nodes than element nodes possible in the DOM, in your
    case there are text nodes with white space between the element nodes.

    Access element nodes using getElementsByTagName e.g.
    NodeList items = xmlDocument.getElementsByTagName("item");
    for (int i = 0; i < items.getLength(); i++) {
    Element item = (Element)items.item(i);
    Element name = (Element)item.getElementsByTagName("name").item(0);
    if (name != null) {
    // access contents of name element
    // e.g. if that is Java Sun 1.5
    // name.getTextContent()
    // if that is 1.4
    // name.getFirstChild().getNodeValue()
    }
    Element sequence =
    (Element)item.getElementsByTagName("sequence").item(0);
    if (sequence != null) {
    ...
    }
    }
    If you have several child elements you can of course loop over e.g.
    item.getElementsByTagName("name")
    or
    item.getElementsByTagName("sequence")


    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Feb 2, 2006
    #2
    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. Ulf Meinhardt
    Replies:
    0
    Views:
    438
    Ulf Meinhardt
    Nov 11, 2004
  2. Replies:
    0
    Views:
    2,928
  3. arne
    Replies:
    0
    Views:
    377
  4. Sean
    Replies:
    3
    Views:
    344
    robic0
    Oct 3, 2006
  5. Sean
    Replies:
    0
    Views:
    394
Loading...

Share This Page