[XML-DEV Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Using Java for DOM processing


using dom in java
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


PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
 

Stylus Studio has published XML-DEV in RSS and ATOM formats, enabling users to easily subcribe to the list from their preferred news reader application.


Stylus Studio Sponsored Links are added links designed to provide related and additional information to the visitors of this website. they were not included by the author in the initial post. To view the content without the Sponsor Links please click here.

Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.