DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   Java (http://www.daniweb.com/forums/forum9.html)
-   -   Eliminating Whitespace characters while parsing XML Files using DOM (http://www.daniweb.com/forums/thread68588.html)

parthiban Jan 29th, 2007 12:02 pm
Eliminating Whitespace characters while parsing XML Files using DOM
 
Hi all ,

I'm having problem during parsing xml files using DOM API due to whitespace characters.

Here is my sample XML File ,coding and output :

XML File :

<demo>
    <empid>e100</empid>
    </demo>
Coding :

import java.io.*;
import org.w3c.dom.*;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;


import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;

class demo{
    public static void main(String args[]) throws IOException {
        try {
        DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
        Document doc = docBuilder.parse(new File("C:/Documents and Settings/t48ezi/Desktop/demo.xml"));
       
        Element rootElement = doc.getDocumentElement();
        System.out.println("root ==> "+rootElement.getNodeName());
        NodeList sublist = rootElement.getChildNodes();
        System.out.println("sublistlength --> "+sublist.getLength());
        System.out.println("sub name 0 :: "+sublist.item(0).getNodeName());
        System.out.println("sub name 1 :: "+sublist.item(1).getNodeName());
        System.out.println("sub name 2 :: "+sublist.item(2).getNodeName());
       
        }
        catch(Exception e){}
    }
}
Output :

root ==> demo
sublistlength --> 3
sub name 0 :: #text
sub name 1 :: empid
sub name 2 :: #text
I have only one chid but i got the number of child of rootelement as "3" due to whitespace character as shown in output(#text) but when i manually type XML Files in LINEAR fashion means i'm getting correct output .

LINEAR FASHION XML FILE:

<demo><empid>e100</empid></demo>
but i used a code that automatically generate xml format . So , how to eliminate that (#text) ?

Thanks

jwenting Jan 29th, 2007 1:18 pm
Re: Eliminating Whitespace characters while parsing XML Files using DOM
 
You don't. You rather learn to use the XML parser properly and explicitly read only those elements from the tree that you actually need.


All times are GMT -4. The time now is 12:52 pm.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC