我正在嘗試爲從Web下載的XHTML文檔編寫SAX解析器。起初,我是有DOCTYPE聲明一個問題(我從here,這是因爲W3C特意禁止訪問DTD發現),但我固定的搭配:Java - XHTML文檔中的SAX解析器
XMLReader reader = parser.getXMLReader();
reader.setFeature("http://apache.org/xml/features/disallow-doctype-decl",true);
不過,現在我遇到第二個問題。當它到達一些JavaScript嵌入在XHTML文檔中的SAX解析器拋出異常:
<script type="text/javascript" language="JavaScript">
function checkForm() {
answer = true;
if (siw && siw.selectingSomething)
answer = false;
return answer;
}//
</script>
具體一旦到達& &的,因爲它的預期實體引用解析器拋出一個錯誤。確切的例外是:
`org.xml.sax.SAXParseException: The entity name must immediately follow the '&' in the entity reference.
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:198)
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.fatalError(ErrorHandlerWrapper.java:177)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:391)
at com.sun.org.apache.xerces.internal.impl.XMLScanner.reportFatalError(XMLScanner.java:1390)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanEntityReference(XMLDocumentFragmentScannerImpl.java:1814)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:3000)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:624)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:486)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:810)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:740)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:110)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1208)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:525)
at MLIAParser.readPage(MLIAParser.java:55)
at MLIAParser.main(MLIAParser.java:75)`
我懷疑(但我不知道),如果我沒有禁用DTD然後我就不會得到這個錯誤。那麼,我該如何避免DTD錯誤並避免實體引用錯誤?
乾杯,
皮特
我沒有關閉DTD,而是下載了它,並將其作爲嵌入資源添加到我的軟件中;那麼,當解析器需要它的時候,我將它的本地/下載/緩存的DTD拷貝給它,而不是從互聯網上獲取它。我認爲這比完全禁用DTD處理更好。 – ChrisW 2009-08-16 13:27:48