2016-08-21 55 views
1

我想分析一些HTML字符串org.w3c.dom.Document中,我使用這種方法:的Java XML解析器例外:結束標記爲元素類型「欄」必須以一個「>」結束定界符

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字符串有

<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> 

異常是由方法拋出 「COLGROUP」 和 「col」 標記(如以下)是:

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標籤的語法是正確的,我不知道如何解決這個問題。

回答

相關問題