[Sax-devel] Resolve all entities
Brought to you by:
dmegginson
From: Vallone, P. Mr C. U. A. <Phi...@us...> - 2008-04-24 13:07:08
|
Hi, I was wondering if you could provide some guidance on resolving entities before they are processed. I have the following java code that will list an entities systemIDs referenced in an XML document. This works well except if I have an entity referenced that references a list of entities (like a catalog of entities) or an entity that is not used. The following XML returns: System ID: file:/C:/path/driverlicense.dtd System ID: file:/C:/path/phone.ent Since "commonEnties" was not used, nothing is returned. How do I return all entites files referenced? ********code*********** Sample XML test.xml <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE DRIVERSLICENSE SYSTEM "driverlicense.dtd" [ <!ENTITY phone SYSTEM "phone.ent"> <!ENTITY % commonEnties SYSTEM 'c:/temp/common.ent'> %commonEnties; ]> <?xml-stylesheet type="text/xsl" href="driverslicense.xslt"?> <DRIVERSLICENSE> <NAME nickname="Joey">JOE</NAME> <STREET>1313 Mocking Bird</STREET> <CITY>Here</CITY> <STATE>NY</STATE> <ZIP>11738</ZIP> <CELL>123-456-7890</CELL> ☎ </DRIVERSLICENSE> Java code: private void resolver(java.awt.event.ActionEvent evt) { try { // Create an XML parser DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); // Install the entity resolver builder.setEntityResolver(new MyResolver()); // Parse the XML file org.w3c.dom.Document doc = builder.parse(new File("C:/path/test.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); System.out.println("System ID: " + systemId); // Check if external source is a file if ("file".equals(uri.getScheme())) { String filename = uri.getSchemeSpecificPart(); return new InputSource(new FileReader(filename)); } else { } } catch (URISyntaxException e) { System.err.println(e.toString()); } catch (IOException e) { } // Returning null causes the caller to try accessing the systemid return null; } } Thanks in advance, Phil |