DTD parsing with Stax
i want to parse xml files which declare a HTML 4.01 Doctype.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
[...]
</html>
I using Stax and an XMLResolver for load local dtd
XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance();
xmlInputFactory.setXMLResolver(new LocalXmlResolver());
xmlOutputFactory = XMLOutputFactory.newInstance();
xmlOutputFactory.createXMLEventWriter(...)
private static final Map<String, String> DTDS = new HashMap<String, String>(){{
// XHTML 1.0 DTDs
put("-//W3C//DTD XHTML 1.0 Strict//EN", "xhtml1-strict.dtd");
put("-//W3C//DTD XHTML 1.0 Transitional//EN", "xhtml1-transitional.dtd");
put("-//W3C//DTD XHTML 1.0 Frameset//EN", "xhtml1-frameset.dtd");
put("-//W3C//DTD HTML 4.01//EN", "strict.dtd");
put("-//W3C//DTD HTML 4.01 Transitional//EN", "loose.dtd");
put("-//W3C//DTD HTML 4.01 Frameset//EN", "frameset.dtd");
}};
private static final class LocalXmlResolver implements XMLResolver {
@Override
public Object resolveEntity(String publicID, String systemID, String baseURI, String namespace) throws XMLStreamException {
Object result = null;
String path = XHTML_DTD_PATH + DTDS.get(publicID);
if (StringUtils.isNotBlank(path)) {
result = getClass().getClassLoader().getResourceAsStream(path);
}
return result;
}
}
i retrieved dtd from the (w3c web site). But i had to change this file to remove all comments in nodes like below :
<!ENTITY % ContentType "CDATA"
-- media type, as per [RFC2045]
-->
<!ENTITY % ContentType "CDATA">
But even after these modifications, i have still this error :
javax.xml.stream.XMLStreamException: ParseError at [row,col]:[184,11]
Message: The element type is required in the element type declaration.
[...]
Caused by: javax.xml.stream.XMLStreamException: ParseError at [row,col]:[184,11]
Message: The element type is required in the element type declaration.
at com.sun.org.apache.xerces.internal.impl.XMLStreamReaderImpl.next(XMLStreamReaderImpl.java:598)
at com.sun.xml.internal.stream.XMLEventReaderImpl.nextEvent(XMLEventReaderImpl.java:83)
in the dtd file, the line 184 is :
<!ELEMENT (%fontstyle;|%phrase;) - - (%inline;)* >
any idea ?
Thanks
HTML is an SGML language, so it has an SGML DTD. You can find some more information about SGML here: http://validator.w3.org/docs/sgml.html
SGML is a bit different than XML, so it's no wonder that an XML parser cannot parse it.
The main example is:
comments inside entity declarations (delimited with double hyphens: --this is a comment--) is allowed in SGML DTD whereas is not on XML DTD.
For more difference please follow http://www.w3.org/TR/NOTE-sgml-xml-971215#null
Nevertheless you can't disable DTD parsing for specific DTD by creation your own XMLResolver
xmlInput = XMLInputFactory.newInstance();
xmlInput.setXMLResolver(new XMLResolver() {
@Override
public Object resolveEntity(String publicID, String systemID, String baseURI, String namespace) throws XMLStreamException {
...
// Disable dtd validation
if ("The public id you except".equals(publicId)) {
return IOUtils.toInputStream("");
}
...
}
});
For html parser consider http://jtidy.sourceforge.net/ or http://jsoup.org/ as solution
链接地址: http://www.djcxy.com/p/34902.html上一篇: 使DocumentBuilder.parse忽略DTD引用
下一篇: 使用Stax进行DTD分析