different between "getDocumentElement" and "getFirstChild"
The Node you get using myDoc.getFirstChild() may not be the document root if there are other nodes before the document root node - such as a comment node. Look at the example below:
import org.w3c.dom.*;public class ReadXML { public static void main(String args[]) throws Exception{ DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = docFactory.newDocumentBuilder(); // Document elements Document doc = docBuilder.parse(new File(args[0])); Node firstChild = doc.getFirstChild(); System.out.println(firstChild.getChildNodes().getLength()); System.out.println(firstChild.getNodeType()); System.out.println(firstChild.getNodeName()); Node root = doc.getDocumentElement(); System.out.println(root.getChildNodes().getLength()); System.out.println(root.getNodeType()); System.out.println(root.getNodeName()); }}
When parsing the following XML file:
<?xml version="1.0"?><!-- Edited by XMLSpy --><catalog> <product description="Cardigan Sweater" product_image="cardigan.jpg"> <catalog_item gender="Men's"> <item_number>QWZ5671</item_number> <price>39.95</price> <size description="Medium"> <color_swatch image="red_cardigan.jpg">Red</color_swatch> <color_swatch image="burgundy_cardigan.jpg">Burgundy</color_swatch> </size> <size description="Large"> <color_swatch image="red_cardigan.jpg">Red</color_swatch> <color_swatch image="burgundy_cardigan.jpg">Burgundy</color_swatch> </size> </catalog_item> </product></catalog>
gives the following result:
08#comment31catalog
But if I remove the comment, it gives:
31catalog31catalog