Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Document from DOMParser vs. from DocumentBuilder

843834May 4 2005 — edited May 11 2005
Dear 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!
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 8 2005
Added on May 4 2005
3 comments
226 views