Java: how to prevent ‘systemid’ #resolveentity (string PublicID, string systemid) in entityresolver from being absolute to the current working directory
I want to parse the following XML document to parse all entities in it:
<!DOCTYPE doc SYstem 'mydoc.dtd'> <doc>&title;</doc>
My entityresolver should get the external entity with the given system id from the database and execute the solution. See the following illustration:
private static class MyEntityResolver { public InputSource resolveEntity(String publicId,String systemId) throws SAXException,IOException { // At this point,systemId is always absolutized to the current working directory,// even though the XML document specified it as relative. // E.g. "file:///H:/mydoc.dtd" instead of just "mydoc.dtd" // Why??? How can I prevent this??? SgmlEntity entity = findEntityFromDatabase(systemId); InputSource is = new InputSource(new ByteArrayInputStream(entity.getContents())); is.setPublicId(publicId); is.setSystemId(systemId); return is; } }
I try to use DOM (documentbuilder) and sax (xmlreader) to set the entity parser to myentityresolver (i.e. setentityresolver (New myentityresolver())), but the systemid #resolveentity (string PublicID, string systemid) in myentityresolver is always absolute to the current working directory
I also tried calling setfeature(“ http://xml.org/sax/features/resolve-dtd-uris ”,false); But it doesn't help anything
So how can I achieve my goal?
thank you!
Solution
Obviously, there is another interface called entityresolver2, which is an extension of the old entityresolver (talking about confusing names!)
In any case, I found that entityresolver2 implements what I want, that is, it will not make any changes to the systemid, so it will always be exactly what is specified in the XML document