| | |
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 |
2dgraphics @param account affinetransform android api apple applet application arc arguments array automation banking binary binarytree bluetooth chatprogramusingobjects class client code color compare component count database derby design detection eclipse eclipsedevelopment encryption error fractal game givemetehcodez graphics gridlayout gui guitesting helpwithhomework html ide if_statement image inheritance j2me java java.xls javadesktopapplications javaprojects jlabel jni jpanel julia keytool keyword linux list macintosh map method methods midlethttpconnection mobile monitoring netbeans nullpointerexception object open-source pong problem producer program project projectideas property reference replaysolutions ria rim scanner server set size sms sourcelabs splash sql stop swing terminal threads transforms tree ui unicode validation web windows






