944,044 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 6073
  • Java RSS
Jan 29th, 2007
0

Eliminating Whitespace characters while parsing XML Files using DOM

Expand Post »
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 :

Java Syntax (Toggle Plain Text)
  1. <demo>
  2. <empid>e100</empid>
  3. </demo>
Coding :

Java Syntax (Toggle Plain Text)
  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 :

Java Syntax (Toggle Plain Text)
  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:

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

Thanks
Similar Threads
Reputation Points: 10
Solved Threads: 6
Junior Poster in Training
parthiban is offline Offline
80 posts
since Sep 2006
Jan 29th, 2007
0

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.
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Jumping on to Java
Next Thread in Java Forum Timeline: deploying application on JBoss





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC