本文介绍了将文档转换为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们来看看这个链接:





我和他有同样的问题,但他无法解决它。我不想从JBrowser改为DJ Project。我很好奇这个问题。任何想法这个问题有什么问题?



谢谢!



编辑: / p>

HTML文件:



这是将文档转换为字符串的方法

  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();
变压器变压器= 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(); (org.jsoup.nodes.Element td:rowElements)
{
System.out.println(td.text());
k = k - 1;
}

错误

 错误:'前缀'的命名空间'collapse-table'未被声明'
javax.xml.transform.TransformerException:java。 lang.RuntimeException:前缀collapse; table-layout的命名空间尚未声明。
在com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:736)
在com.sun.org.apache.xalan.internal.xsltc。 trax.TransformerImpl.transform(TransformerImpl.java:340)
在browser.component.JBrowser.getStringFromDocument(JBrowser.java:428)
在browser.component.JBrowser $ 9.actionPerformed(JBrowser.java:133 )
在javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
在javax.swing.AbstractButton $ Handler.actionPerformed(AbstractButton.java:2341)
在javax.swing。 DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
在javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
在javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java: 252)
在java.awt.Component.processMouseEvent(Component.java:6505)
在javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
在java.awt.Component .processEvent(Component.java:6270)
在java.awt .Container.processEvent(Container.java:2229)
在java.awt.Component.dispatchEventImpl(Component.java:4861)
在java.awt.Container.dispatchEventImpl(Container.java:2287)
在java.awt.Component.dispatchEvent(Component.java:4687)
在java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
在java.awt.LightweightDispatcher.processMouseEvent( Container.java:4492)
在java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
在java.awt.Container.dispatchEventImpl(Container.java:2273)
在java .awt.Window.dispatchEventImpl(Window.java:2719)
在java.awt.Component.dispatchEvent(Component.java:4687)
在java.awt.EventQueue.dispatchEventImpl(EventQueue.java:729 )
在java.awt.EventQueue.access $ 200(EventQueue.java:103)
在java.awt.EventQueue $ 3.run(EventQueue.java:688)
在java.awt。 EventQueue $ 3.run(EventQueue.java:686)
在java.security.AccessController.doPrivileged(Native方法)
在java.security.ProtectionDomain $ 1.doIntersectionPrivilege(ProtectionDomain.java:76)
在java.security.ProtectionDomain $ 1.doIntersectionPrivilege(ProtectionDomain.java:87)
在java.awt.EventQueue $ 4.run(EventQueue.java:702)
在java.awt.EventQueue $ 4.run(EventQueue.java:700)
在java.security.AccessController.doPrivileged(本机方法)
在java.security.ProtectionDomain $ 1.doIntersectionPrivilege( ProtectionDomain.java:76)
在java.awt.EventQueue.dispatchEvent(EventQueue.java:699)
在java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
在java .awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
在java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
在java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146 )
在java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
在java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
导致:java.lang。 Runtim eException:前缀collapse; table-layout的命名空间尚未声明。
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)
在com.sun.org.apache.xml.internal.serializer.ToUnknownStream.addAttribute(ToUnknownStream.java:316)
在com.sun.org.apache。 xalan.internal.xsltc.trax.DOM2TO.parse(DOM2TO.java:201)
在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)
在com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.parse(DOM2TO.java:136)
在com.sun .org.apache.xalan.internal.xsltc.trax.DOM2TO.parse(DOM2TO.java:98)
在com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transformIdentity(TransformerImpl。 java:683)
在com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.tran sform(TransformerImpl.java:727)



...更多


解决方案

您正在使用XML工具来处理HTML,因此您需要首先将HTML转换为XML。您可以使用JTidy或TagSoup来实现。


Let's see this link :

HTML DOM Tree to String - Transformer NullPointerException

I got the same problem with him..but he can't solve it. I don't want to change from JBrowser to DJ Project. I'm curious with this problem. Any idea what's wrong with this problem?

Thanks!

Edit:

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

This is method to transform document to string

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();
}

This is method to get specific element :

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 :

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 more

解决方案

You're using XML tools to process HTML, so you need to start by converting the HTML to XML. You can do that using for example JTidy or TagSoup.

这篇关于将文档转换为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 20:24