Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / February 2006

Tip: Looking for answers? Try searching our database.

Java Sun XML parser weird behaviour

Thread view: 
Dennis - 02 Feb 2006 05:38 GMT
<?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
Martin Honnen - 02 Feb 2006 12:46 GMT
> <database>
>     <item>
[quoted text clipped - 12 lines]
> 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")

Signature

    Martin Honnen
    http://JavaScript.FAQTs.com/



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.