How do I convert a org.w3c.dom.Document object to a String?

匿名 (未验证) 提交于 2019-12-03 02:14:01

问题:

I want to convert a org.w3c.dom.Document object to a String. I'm using Java 6 and am open to using any (completely free) technology that is up to the task. I tried the solution from this thread -- Is there a more elegant way to convert an XML Document to a String in Java than this code?, where they have

DOMImplementationLS domImplementation = (DOMImplementationLS) doc.getImplementation(); LSSerializer lsSerializer = domImplementation.createLSSerializer(); String html = lsSerializer.writeToString(doc);  

but was greeted with the following horrendous exception …

org.w3c.dom.DOMException: DOM method not supported     at org.w3c.tidy.DOMDocumentImpl.getImplementation(DOMDocumentImpl.java:129)     at com.myco.myproj.cleaners.JTidyCleaner.outputDocAsString(JTidyCleaner.java:74)     at com.myco.myproj.cleaners.JTidyCleaner.parse(JTidyCleaner.java:63)     at com.myco.myproj.util.NetUtilities.getUrlAsDocument(NetUtilities.java:51)     at com.myco.myproj.parsers.AbstractHTMLParser.getEventFromElement(AbstractHTMLParser.java:131)     at com.myco.myproj.parsers.AbstractHTMLParser.parsePage(AbstractHTMLParser.java:100)     at com.myco.myproj.parsers.AbstractHTMLParser.getEvents(AbstractHTMLParser.java:63)     at com.myco.myproj.domain.EventFeed.refresh(EventFeed.java:87)     at com.myco.myproj.domain.EventFeed.getEvents(EventFeed.java:72)     at com.myco.myproj.parsers.impl.ChicagoCouncilGlobalAffairsParserTest.testParser(ChicagoCouncilGlobalAffairsParserTest.java:21)     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)     at java.lang.reflect.Method.invoke(Method.java:597)     at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)     at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)     at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)     at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)     at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)     at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)     at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83)     at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)     
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!