OASIS Mailing List ArchivesView the OASIS mailing list archive below
or browse/search using MarkMail.

 


Help: OASIS Mailing Lists Help | MarkMail Help

 


 

   Using Java for DOM processing

[ Lists Home | Date Index | Thread Index ]

Hi,

I am currently trying to get my head round using DOM with Java. In 
particular I have been looking at the getNodeName() method. I have noticed 
that the format/layout
you present the XML document in, seems to have an effect on the way the DOM 
method (getNodeName) handles the XML document. Could someone enlighten me on 
this,
is this a bug or something normal and if so should I expect all DOM methods 
to process XML documents in a manner dependent on the XML format/layout?

Below I have presented a snippet of the Java code and XML document I am 
dealing with to help you "digest" my problem. Your help is appreciated!


Java code snippet >>>
...
         System.out.print( "Here is the document's root node:" );
         System.out.println( " " + root.getNodeName() );

         System.out.println( "Here are its child elements: " );
         NodeList childNodes = root.getChildNodes();
         Node currentNode;
...

         for ( int i = 0; i < childNodes.getLength(); i++ ) {

            currentNode = childNodes.item( i );

            // print node name of each child element
            System.out.println( currentNode.getNodeName());
         }
...


This is the output I desire :

Here is the document's root node: article
Here are its child elements:
title
date
author
summary
content
The first child of root node is: title
whose next sibling is: date
value of date element is: April 13, 2004
Parent node of date is: article

I only get the above output when my XML file is presented as below:

<?xml version = "1.0"?>
<article><title>Simple XML</title><date>April 13, 
2004</date><author><firstName>Tem</firstName><lastName>Nieto</lastName></author><summary>XML 
is easy.</summary><content>Once you have mastered XHTML, you can easily 
learn XML. You must remember that XML is not for displaying information but 
for managing information.</content></article>


When my XML file is foramtted as below this is the (unwanted) output I get:

Here is the document's root node: article
Here are its child elements:
#text
title
#text
date
#text
author
#text
summary
#text
content
#text
The first child of root node is: #text
whose next sibling is: title
value of title element is: Simple XML
Parent node of title is: article


This is the XML layout I prefer to use, note each element is defined on a 
new line, unfortunately it gives me the (unwanted) output above i.e. the 
#text:

<?xml version = "1.0"?>
<article>
<title>Simple XML</title>
<date>April 13, 2004</date>
<author>
<firstName>Tem</firstName>
<lastName>Nieto</lastName>
</author>
<summary>XML is easy.</summary>
<content>Once you have mastered XHTML, you can easily learn XML. You must 
remember that XML is not for displaying information but for managing 
information.
</content>
</article>

_________________________________________________________________
Express yourself with cool new emoticons http://www.msn.co.uk/specials/myemo





 

News | XML in Industry | Calendar | XML Registry
Marketplace | Resources | MyXML.org | Sponsors | Privacy Statement

Copyright 2001 XML.org. This site is hosted by OASIS