您好在我的代码中运行以下行,但此代码在执行期间崩溃。
ByteArrayInputStream input = new ByteArrayInputStream(fileContent);
final HtmlCleaner cleaner = new HtmlCleaner();
CleanerProperties props = cleaner.getProperties();
DomSerializer doms = new DomSerializer(props, true);
org.w3c.dom.Document xmlDoc = null;
try {
TagNode node = cleaner.clean(input);
xmlDoc = doms.createDOM(node);
} catch (Exception e) {
System.out.println("Tiding error ");
e.printStackTrace();
}
这是错误的堆栈跟踪:
NAMESPACE_ERR: An attempt is made to create or change an object in a way which is incorrect with regard to namespaces.
at com.sun.org.apache.xerces.internal.dom.CoreDocumentImpl.checkDOMNSErr(CoreDocumentImpl.java:2535)
at com.sun.org.apache.xerces.internal.dom.AttrNSImpl.setName(AttrNSImpl.java:113)
at com.sun.org.apache.xerces.internal.dom.AttrNSImpl.<init>(AttrNSImpl.java:74)
at com.sun.org.apache.xerces.internal.dom.CoreDocumentImpl.createAttributeNS(CoreDocumentImpl.java:2138)
at com.sun.org.apache.xerces.internal.dom.ElementImpl.setAttributeNS(ElementImpl.java:656)
at org.htmlcleaner.DomSerializer.setAttributes(DomSerializer.java:97)
at org.htmlcleaner.DomSerializer.createDOM(DomSerializer.java:37)
任何人都可以帮忙找出原因吗?
此致,Zoli
答案 0 :(得分:0)
HTMLCleaner遇到了处理命名空间的问题。下面是一个XML命名空间声明示例,它会给它带来麻烦:
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="de" lang="de"
xmlns:og="http://ogp.me/ns#" xmlns:fb="http://www.facebook.com/2008/fbml"
itemscope itemtype="http://schema.org/CreativeWork">
您可以看到itemscope属性已损坏,因此HtmlCleaner会抛出NAME_SPACE_ERR。
避免此问题的一种方法是添加行
props.setNamespacesAware(false);
关闭命名空间处理。