Java 13 DocumentBuilder 在解析 DTD 文件以验证时中断 HTML
Java 13 DocumentBuilder breaks when parsing DTD file to validate HTML
我正在开发使用 DocumentBuilder 解析旧 HTML 文件的程序,以便对其进行相应处理。在这个 HTML 文件中,我们有以下
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
这是执行读取的代码片段:
DocumentBuilderFactory documentBuilderFactory;
DocumentBuilder documentBuilder;
documentBuilderFactory = DocumentBuilderFactory.newInstance();
documentBuilder = documentBuilderFactory.newDocumentBuilder();
Document doc = documentBuilder.parse(htmlSource);
随后解析失败并出现以下错误:
Error 1: The declaration for the entity "HTML.Version" must end with '>'.
Column Number: 3
System Identifer: null
toString: org.xml.sax.SAXParseException; lineNumber: 31; columnNumber: 3; The declaration for the entity "HTML.Version" must end with '>'.
Line Number: 31
Public Identifer: null
Caused By:
The declaration for the entity "HTML.Version" must end with '>'.
Trace Follows:
org.xml.sax.SAXParseException; lineNumber: 31; columnNumber: 3; The declaration for the entity "HTML.Version" must end with '>'.
at java.xml/com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:204)
at java.xml/com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.fatalError(ErrorHandlerWrapper.java:178)
at java.xml/com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:400)
at java.xml/com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:327)
at java.xml/com.sun.org.apache.xerces.internal.impl.XMLScanner.reportFatalError(XMLScanner.java:1471)
at java.xml/com.sun.org.apache.xerces.internal.impl.XMLDTDScannerImpl.scanEntityDecl(XMLDTDScannerImpl.java:1597)
at java.xml/com.sun.org.apache.xerces.internal.impl.XMLDTDScannerImpl.scanDecls(XMLDTDScannerImpl.java:2021)
at java.xml/com.sun.org.apache.xerces.internal.impl.XMLDTDScannerImpl.scanDTDExternalSubset(XMLDTDScannerImpl.java:299)
at java.xml/com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$DTDDriver.dispatch(XMLDocumentScannerImpl.java:1165)
at java.xml/com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$DTDDriver.next(XMLDocumentScannerImpl.java:1040)
at java.xml/com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(XMLDocumentScannerImpl.java:943)
at java.xml/com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:605)
at java.xml/com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:541)
at java.xml/com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:888)
at java.xml/com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:824)
at java.xml/com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:141)
at java.xml/com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:246)
at java.xml/com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:339)
at com.rockwellcollins.ana.xml.XmlParser.parse(XmlParser.java:490)
at com.rockwellcollins.ana.xml.XmlParser.parse(XmlParser.java:592)
at com.rockwellcollins.qimt.doorsmapper.doorsmapper.HtmlParser.parseHtml(HtmlParser.java:301)
at com.rockwellcollins.qimt.doorsmapper.doorsmapper.DoorsMapper.applicationSpecificDoIt(DoorsMapper.java:232)
at com.rockwellcollins.application.common.ApplicationBase.doIt(ApplicationBase.java:795)
at com.rockwellcollins.qimt.doorsmapper.doorsmapper.DoorsMapper.main(DoorsMapper.java:300)
它抱怨 DTD 文件的这一部分:
<!ENTITY % HTML.Version "-//W3C//DTD HTML 4.01 Transitional//EN"
-- Typical usage:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
...
</head>
<body>
...
</body>
</html>
The URI used as a system identifier with the public identifier allows
the user agent to download the DTD and entity sets as needed.
The FPI for the Strict HTML 4.01 DTD is:
"-//W3C//DTD HTML 4.01//EN"
This version of the strict DTD is:
http://www.w3.org/TR/1999/REC-html401-19991224/strict.dtd
Authors should use the Strict DTD unless they need the
presentation control for user agents that don't (adequately)
support style sheets.
If you are writing a document that includes frames, use
the following FPI:
"-//W3C//DTD HTML 4.01 Frameset//EN"
This version of the frameset DTD is:
http://www.w3.org/TR/1999/REC-html401-19991224/frameset.dtd
Use the following (relative) URIs to refer to
the DTDs and entity definitions of this specification:
"strict.dtd"
"loose.dtd"
"frameset.dtd"
"HTMLlat1.ent"
"HTMLsymbol.ent"
"HTMLspecial.ent"
-->
根据我的初步调查,它抱怨标签中的 --
评论。如果我删除它们,那么第一个错误就会消失并移至下一个错误。我的问题是,为什么 DocumentBuilder 无法正确读取 DTD 文件?
要添加一些东西,我们无法从 HTML 文件中删除 DTD,因为提供的 HTML 是 HTML 4 特定的,没有它,解析将失败,因为HTML 4 格式。
HTML 4.01 是一个 SGML DTD(XML 是 SGML 的一个子集)并且 HTML 无法使用XML 解析器。与 XML 相比,SGML 中的注释语法允许注释在任何地方和多次出现在标记声明中,这是对的。例如,以下是有效的 SGML 元素声明:
<!ELEMENT e - - (#PCDATA)
-- declaration for e --
-- ... other comment -->
该声明还暗示了 SGML 的 XML 子集不支持的功能之一(但解析 HTML 需要),即 标签推断(标签遗漏)。元素名称 e
后的 - O
序列表示 e
允许省略结束元素标记("O",如字母 O)但不允许省略起始元素(“-”连字符减号)。 XML 不支持的其他所需功能是 SGML/HTML-style 空元素,例如 img
(没有结束元素标记)和属性最小化(如 <div hidden>
)。
我正在开发使用 DocumentBuilder 解析旧 HTML 文件的程序,以便对其进行相应处理。在这个 HTML 文件中,我们有以下
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
这是执行读取的代码片段:
DocumentBuilderFactory documentBuilderFactory;
DocumentBuilder documentBuilder;
documentBuilderFactory = DocumentBuilderFactory.newInstance();
documentBuilder = documentBuilderFactory.newDocumentBuilder();
Document doc = documentBuilder.parse(htmlSource);
随后解析失败并出现以下错误:
Error 1: The declaration for the entity "HTML.Version" must end with '>'.
Column Number: 3
System Identifer: null
toString: org.xml.sax.SAXParseException; lineNumber: 31; columnNumber: 3; The declaration for the entity "HTML.Version" must end with '>'.
Line Number: 31
Public Identifer: null
Caused By:
The declaration for the entity "HTML.Version" must end with '>'.
Trace Follows:
org.xml.sax.SAXParseException; lineNumber: 31; columnNumber: 3; The declaration for the entity "HTML.Version" must end with '>'.
at java.xml/com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:204)
at java.xml/com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.fatalError(ErrorHandlerWrapper.java:178)
at java.xml/com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:400)
at java.xml/com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:327)
at java.xml/com.sun.org.apache.xerces.internal.impl.XMLScanner.reportFatalError(XMLScanner.java:1471)
at java.xml/com.sun.org.apache.xerces.internal.impl.XMLDTDScannerImpl.scanEntityDecl(XMLDTDScannerImpl.java:1597)
at java.xml/com.sun.org.apache.xerces.internal.impl.XMLDTDScannerImpl.scanDecls(XMLDTDScannerImpl.java:2021)
at java.xml/com.sun.org.apache.xerces.internal.impl.XMLDTDScannerImpl.scanDTDExternalSubset(XMLDTDScannerImpl.java:299)
at java.xml/com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$DTDDriver.dispatch(XMLDocumentScannerImpl.java:1165)
at java.xml/com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$DTDDriver.next(XMLDocumentScannerImpl.java:1040)
at java.xml/com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(XMLDocumentScannerImpl.java:943)
at java.xml/com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:605)
at java.xml/com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:541)
at java.xml/com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:888)
at java.xml/com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:824)
at java.xml/com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:141)
at java.xml/com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:246)
at java.xml/com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:339)
at com.rockwellcollins.ana.xml.XmlParser.parse(XmlParser.java:490)
at com.rockwellcollins.ana.xml.XmlParser.parse(XmlParser.java:592)
at com.rockwellcollins.qimt.doorsmapper.doorsmapper.HtmlParser.parseHtml(HtmlParser.java:301)
at com.rockwellcollins.qimt.doorsmapper.doorsmapper.DoorsMapper.applicationSpecificDoIt(DoorsMapper.java:232)
at com.rockwellcollins.application.common.ApplicationBase.doIt(ApplicationBase.java:795)
at com.rockwellcollins.qimt.doorsmapper.doorsmapper.DoorsMapper.main(DoorsMapper.java:300)
它抱怨 DTD 文件的这一部分:
<!ENTITY % HTML.Version "-//W3C//DTD HTML 4.01 Transitional//EN"
-- Typical usage:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
...
</head>
<body>
...
</body>
</html>
The URI used as a system identifier with the public identifier allows
the user agent to download the DTD and entity sets as needed.
The FPI for the Strict HTML 4.01 DTD is:
"-//W3C//DTD HTML 4.01//EN"
This version of the strict DTD is:
http://www.w3.org/TR/1999/REC-html401-19991224/strict.dtd
Authors should use the Strict DTD unless they need the
presentation control for user agents that don't (adequately)
support style sheets.
If you are writing a document that includes frames, use
the following FPI:
"-//W3C//DTD HTML 4.01 Frameset//EN"
This version of the frameset DTD is:
http://www.w3.org/TR/1999/REC-html401-19991224/frameset.dtd
Use the following (relative) URIs to refer to
the DTDs and entity definitions of this specification:
"strict.dtd"
"loose.dtd"
"frameset.dtd"
"HTMLlat1.ent"
"HTMLsymbol.ent"
"HTMLspecial.ent"
-->
根据我的初步调查,它抱怨标签中的 --
评论。如果我删除它们,那么第一个错误就会消失并移至下一个错误。我的问题是,为什么 DocumentBuilder 无法正确读取 DTD 文件?
要添加一些东西,我们无法从 HTML 文件中删除 DTD,因为提供的 HTML 是 HTML 4 特定的,没有它,解析将失败,因为HTML 4 格式。
HTML 4.01 是一个 SGML DTD(XML 是 SGML 的一个子集)并且 HTML 无法使用XML 解析器。与 XML 相比,SGML 中的注释语法允许注释在任何地方和多次出现在标记声明中,这是对的。例如,以下是有效的 SGML 元素声明:
<!ELEMENT e - - (#PCDATA)
-- declaration for e --
-- ... other comment -->
该声明还暗示了 SGML 的 XML 子集不支持的功能之一(但解析 HTML 需要),即 标签推断(标签遗漏)。元素名称 e
后的 - O
序列表示 e
允许省略结束元素标记("O",如字母 O)但不允许省略起始元素(“-”连字符减号)。 XML 不支持的其他所需功能是 SGML/HTML-style 空元素,例如 img
(没有结束元素标记)和属性最小化(如 <div hidden>
)。