【问题标题】:java xml parser Exception: The end-tag for element type "col" must end with a '>' delimiterjava xml解析器异常:元素类型“col”的结束标记必须以“>”分隔符结尾
【发布时间】:2016-12-27 22:57:25
【问题描述】:

我想将一些 Html 字符串解析为 org.w3c.dom.Document,我使用这种方法:

public static Document stringToDocument(String input){
    try {
        DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        InputSource is = new InputSource();
        is.setCharacterStream(new StringReader(input));
        Document doc = db.parse(is);
        return doc;
    }catch (Exception e){
        e.printStackTrace();
        return null;
    }
}

这在大多数 html 上都可以正常工作,除了 html 字符串有“colgroup”和“col”标签(如下所示)

<html dir="rtl"><head><meta charset="utf-8"/></head>
<body>
<table>
<colgroup>
<col width="29">
<col style="width:54pt" span="4" width="72">
<col width="4">
</colgroup>
<tbody>
<tr>
<td>test</td>
<td>105</td>
<td>110</td>
</tr>
<tr>
<td>456</td>
<td>456</td>
<td>786</td>
</tr>
</tbody>
</table>
</body>
</html>

方法抛出的异常是:

org.xml.sax.SAXParseException; lineNumber: 8; columnNumber: 6; The end-tag for element type "col" must end with a '>' delimiter.
at org.apache.xerces.parsers.DOMParser.parse(Unknown Source)
at org.apache.xerces.jaxp.DocumentBuilderImpl.parse(Unknown Source)

根据w3schools,col标签语法是正确的,我不知道如何解决这个问题。

【问题讨论】:

    标签: java html xml col colgroup


    【解决方案1】:

    问题在于 HTML 不是 XML 格式。请参阅此处http://courses.cs.vt.edu/~cs1204/XML/htmlVxml.html 或此处 http://www.xmlobjective.com/what-is-the-difference-between-xml-and-html/ 或这里 https://webkit.org/blog/68/understanding-html-xml-and-xhtml/ 或使用您最喜欢的搜索引擎并搜索:xml vs html

    顺便说一句。如果你真的想解析 HTML,你可以使用第三方库,如 https://jsoup.org/http://htmlcleaner.sourceforge.net/

    【讨论】:

      猜你喜欢
      • 2015-02-23
      • 2015-10-19
      • 1970-01-01
      • 1970-01-01
      • 2014-07-27
      • 2014-05-26
      • 2020-07-14
      • 2010-09-05
      • 1970-01-01
      相关资源
      最近更新 更多