| | |
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 :
Coding :
Output :
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:
but i used a code that automatically generate xml format . So , how to eliminate that (#text) ?
Thanks
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)
<demo> <empid>e100</empid> </demo>
Java Syntax (Toggle Plain Text)
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){} } }
Java Syntax (Toggle Plain Text)
root ==> demo sublistlength --> 3 sub name 0 :: #text sub name 1 :: empid sub name 2 :: #text
LINEAR FASHION XML FILE:
Java Syntax (Toggle Plain Text)
<demo><empid>e100</empid></demo>
Thanks
![]() |
Similar Threads
- parsing xml (Python)
- What exactly is xml? (RSS, Web Services and SOAP)
- compare 2 xml files with csharp (C#)
- Need some help with parsing an XML file (PHP)
- Java and DOM (Java)
- working with xml files without physically storing them. (ASP.NET)
- Reading stream with two xml files in (Java)
Other Threads in the Java Forum
- Previous Thread: Jumping on to Java
- Next Thread: deploying application on JBoss
| Thread Tools | Search this Thread |
-xlint actionlistener android api applet application array arrays automation bi binary blackberry block bluetooth character class client code compile compiler component consumer database desktop developmenthelp eclipse error fractal freeze ftp functiontesting game gameprogramming givemetehcodez graphics gui health html ide image integer j2me j2seprojects java javac javaee javaprojects jetbrains jni jpanel jtable julia learningresources lego linked linux list mac main map method methods mobile myregfun netbeans notdisplaying number online printf problem program project qt recursion researchinmotion rotatetext rsa scanner screen server set singleton sms sort spamblocker sql string swing system textfields threads time title tree tutorial-sample update variablebinding windows working xor






