SAX Parser: Specify DTD location in applet
I have an applet that works fine in the applet viewer but once deplayed to the server it can't parse the XML files
the reason is simple: the SAX parser is trying to open the dtd on the hard disk and not in the JAR.
I was told to 开发者_开发技巧do this:
URLs to resources can easily be formed using the URL(baseURL, pathString) constructor where the base URL is obtained from Applet.getDocumentBase() or Applet.getCodeBase().
but I don't know how to apply this in my case:
heres a XML sample :
<?xml version="1.0"?>
<!DOCTYPE note SYSTEM "leveldtd.dtd">
<level>
...
</level>
and here is the init of my parser
public static void parseThis(InputSource is) throws Exception{
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLHandlerLevel myExampleHandler = new XMLHandlerLevel();
XMLReader xr = sp.getXMLReader();
xr.setContentHandler(myExampleHandler);
/* Begin parsing */
xr.parse(is);
}
any ideas ?
Jason
If validation is not required you could just turn it of in the SAXParserFactory.setValidating() so the parser won't try to read the DTDs.
If you however do need validation you can redirect any DTD/XSD requests by setting your own EntityResolver on the sax parser.
精彩评论