Hi Michael,
thank you for your reply. In the end, I found a "solution" to my problem.
First of all, I had to call DocumentBuilder.parse(InputStream, String), rather than DocumentBuilder.parse(InputStream) in order to make the parser find schemas, DTDs etc. referenced by the XML file, otherwise it searched for them using my IDE working directory as the base for resolving relative paths...

Once I understood this, I found that the following code can do the job:

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
dbf.setValidating(true);
dbf.setFeature("http://apache.org/xml/features/validation/schema";, true);
DocumentBuilder db = dbf.newDocumentBuilder();
Document document = db.parse(xmlURL.openStream(), xmlURL.toURI().toString());

In this way I don't have to specify a schema (it is automatically taken and parsed by Xerces thanks to the schemaLocation attribute in the XML), but I lose the abstraction from the underlying parser implementation by setting the Xerces feature needed to make schema validation (and getElementById()) work. Please remember that I'm using standard JSE 5 APIs to do the XML parsing.

So, I then decided to write a DTD for the XML and make the parser use it to enable getElementById(), although I don't like the solution so much (actually, having the schema, in this case the DTD is redundant, I use it only to make the parser work as expected, without the need of setting any Xerces-specific feature on the document builder factory). The resulting code is now:

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
DocumentBuilder db = dbf.newDocumentBuilder();
Document document = db.parse(xmlURL.openStream(), xmlURL.toURI().toString());

that actually seems more implementation-independent to me.

Thanks again for your help!

--
Mauro Molinari
Software Developer
[EMAIL PROTECTED]

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to