将文档转换为字符串时出错

时间:2013-07-02 02:11:35

标签: java xml dom transform transformer

让我们看看这个链接:

HTML DOM Tree to String - Transformer NullPointerException

我和他有同样的问题..但他无法解决。我不想从JBrowser更改为DJ Project。我很好奇这个问题。知道这个问题有什么问题吗?

谢谢!

修改

HTML文件:http://www.uploadmb.com/dw.php?id=1372739472

这是将文档转换为字符串

的方法
public String getStringFromDocument(org.w3c.dom.Document doc) {
    StringWriter sw = new StringWriter();
    try {
         doc = browser.getDocument();
         DOMSource domSource = new DOMSource(doc);
         StreamResult result = new StreamResult(sw);
         TransformerFactory tf = TransformerFactory.newInstance();
         Transformer transformer = tf.newTransformer();
         transformer.transform(domSource, result);

    } catch (TransformerConfigurationException e) {
         e.printStackTrace();
    } catch (TransformerException e) {
         e.printStackTrace();
    } catch (TransformerFactoryConfigurationError e) {
         e.printStackTrace();
    }

    return sw.getBuffer().toString();
}

这是获取特定元素的方法:

String html=getStringFromDocument(browser.getDocument());
//org.jsoup.nodes.Document doc;                    
this.doc=Jsoup.parse(html);
org.jsoup.select.Elements tableElements=doc.select("table");
org.jsoup.select.Elements rowElements = tableElements.select("tr");    
int k = rowElements.size();
for (org.jsoup.nodes.Element td : rowElements) {
    System.out.println(td.text());
    k = k - 1;
} 

错误

ERROR:  'Namespace for prefix 'collapse;table-layout' has not been declared.'
javax.xml.transform.TransformerException: java.lang.RuntimeException: Namespace for       prefix 'collapse;table-layout' has not been declared.
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:736)
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:340)
at browser.component.JBrowser.getStringFromDocument(JBrowser.java:428)
at browser.component.JBrowser$9.actionPerformed(JBrowser.java:133)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6505)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:729)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:688)
at java.awt.EventQueue$3.run(EventQueue.java:686)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:702)
at java.awt.EventQueue$4.run(EventQueue.java:700)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:699)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
Caused by: java.lang.RuntimeException: Namespace for prefix 'collapse;table-layout' has not been declared.
at com.sun.org.apache.xml.internal.serializer.SerializerBase.getNamespaceURI(SerializerBase.java:914)
at com.sun.org.apache.xml.internal.serializer.SerializerBase.addAttribute(SerializerBase.java:431)
at com.sun.org.apache.xml.internal.serializer.ToUnknownStream.addAttribute(ToUnknownStream.java:316)
at com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.parse(DOM2TO.java:201)
at com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.parse(DOM2TO.java:230)
at com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.parse(DOM2TO.java:230)
at com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.parse(DOM2TO.java:230)
at com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.parse(DOM2TO.java:136)
at com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.parse(DOM2TO.java:98)
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transformIdentity(TransformerImpl.java:683)
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:727)

... 39更多

2 个答案:

答案 0 :(得分:1)

您正在使用XML工具来处理HTML,因此您需要首先将HTML转换为XML。你可以使用例如JTidy或TagSoup来做到这一点。

答案 1 :(得分:0)

首先通过JTidy(http://jtidy.sourceforge.net/)运行该页面。或者,如果您的解决方案不必灵活,您可以使用子字符串或正则表达式解析所需的数据。