try {
// Create an XML parser
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
// Install the entity resolver
builder.setEntityResolver(new MyResolver());
// Parse the XML file
Document doc = builder.parse(new File("infilename.xml"));
} catch (SAXException e) {
// A parsing error occurred; the xml input is not valid
} catch (ParserConfigurationException e) {
} catch (IOException e) {
}
public class MyResolver implements EntityResolver {
// This method is called whenever an external entity is accessed
// for the first time.
public InputSource resolveEntity (String publicId,String systemId) {
try {
// Wrap the systemId in a URI object to make it convenient
// to extract the components of the systemId
URI uri = new URI(systemId);
// Check if external source is a file
if ("file".equals(uri.getScheme())) {
String filename = uri.getSchemeSpecificPart();
return new InputSource(new FileReader(filename));
}
} catch (URISyntaxException e) {
} catch (IOException e) {
}
// Returning null causes the caller to try accessing the systemid
return null;
}
}
This is the sample input for the example:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE root SYSTEM "System.dtd" [
<!ENTITY entity1 SYSTEM "External.xml">
<!ENTITY entity2 SYSTEM "http://hostname.com/my.dtd">
<!ENTITY % entity3 SYSTEM "More.dtd">
%entity3;
]>
<root>