Resolve all entities
"Vallone, Philip Mr CTR USA AMC" <[email protected]> Thu, 24 Apr 2008 09:06:04 -0400
| Newsgroups | gmane.text.xml.sax.devel |
|---|---|
| Message-ID | <8962628F26A7AA479995C9E374E375F304B3DD0C@MONMBE010C85208.nae.ds.army.mil> |
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
-------------------------------------------------------------------------
This SF.net email is sponsored by the 2008 JavaOne(SM) Conference
Don't miss this year's exciting event. There's still time to save $100.
Use priority code J8TL2D2.
http://ad.doubleclick.net/clk;198757673;13503038;p?http://java.sun.com/javaone
_______________________________________________
List: sax-devel, [email protected]
See: http://www.saxproject.org/
https://lists.sourceforge.net/lists/listinfo/sax-devel