Document from DOMParser vs. from DocumentBuilder
843834May 4 2005 — edited May 11 2005Dear all,
Can anyone tell me why the code below shows such a weired behavior? The XML file used is:
<?xml version="1.0" encoding="ISO-8859-1"?>
<wsml xmlns="myNamespace#">
<ontology>
<axiom>
<definedBy>
<molecule>
....
</molecule>
</definedBy>
</axiom>
</ontology>
</wsml>
/* The following code prints Nb. nodes= 0 */
DOMParser domParser = new DOMParser();
domParser.setFeature("http://xml.org/sax/features/validation", true);
domParser.setFeature("http://apache.org/xml/features/validation/schema",true);
domParser.setProperty(
"http://apache.org/xml/properties/schema/external-schemaLocation",
"myNamespace# myXMLSchema.xsd");
domParser.parse("examples/test.xml");
Document doc = domParser.getDocument();
NodeList exprs = XPathAPI.selectNodeList(doc, "//definedBy/*");
System.out.println("Nb. nodes= " + exprs.getLength());
/*while the following code prints Nb. nodes= 1 */
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setIgnoringElementContentWhitespace(true);
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new File("examples/test.xml"));
NodeList exprs = XPathAPI.selectNodeList(doc, "//definedBy/*");
System.out.println(exprs.getLength());
Could anyone please explain to me the difference between the org.w3c.dom.Document returned by domParser.parse("examples/test.xml"); and the Document returned by db.parse(new File("examples/test.xml"));
Thank you!