Eliminating Whitespace characters while parsing XML Files using DOM

Reply

Join Date: Sep 2006
Posts: 80
Reputation: parthiban is an unknown quantity at this point 
Solved Threads: 6
parthiban's Avatar
parthiban parthiban is offline Offline
Junior Poster in Training

Eliminating Whitespace characters while parsing XML Files using DOM

 
0
  #1
Jan 29th, 2007
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 :

  1. <demo>
  2. <empid>e100</empid>
  3. </demo>
Coding :

  1. import java.io.*;
  2. import org.w3c.dom.*;
  3. import org.w3c.dom.Node;
  4. import org.w3c.dom.NodeList;
  5.  
  6.  
  7. import javax.xml.parsers.DocumentBuilderFactory;
  8. import javax.xml.parsers.DocumentBuilder;
  9.  
  10. class demo{
  11. public static void main(String args[]) throws IOException {
  12. try {
  13. DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
  14. DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
  15. Document doc = docBuilder.parse(new File("C:/Documents and Settings/t48ezi/Desktop/demo.xml"));
  16.  
  17. Element rootElement = doc.getDocumentElement();
  18. System.out.println("root ==> "+rootElement.getNodeName());
  19. NodeList sublist = rootElement.getChildNodes();
  20. System.out.println("sublistlength --> "+sublist.getLength());
  21. System.out.println("sub name 0 :: "+sublist.item(0).getNodeName());
  22. System.out.println("sub name 1 :: "+sublist.item(1).getNodeName());
  23. System.out.println("sub name 2 :: "+sublist.item(2).getNodeName());
  24.  
  25. }
  26. catch(Exception e){}
  27. }
  28. }
Output :

  1. root ==> demo
  2. sublistlength --> 3
  3. sub name 0 :: #text
  4. sub name 1 :: empid
  5. 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:

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

Thanks
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,145
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 212
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: Eliminating Whitespace characters while parsing XML Files using DOM

 
0
  #2
Jan 29th, 2007
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.
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC