如何将org.w3c.dom.Document对象转换为String?


87

我想将org.w3c.dom.Document对象转换为String。我正在使用Java 6,并且愿意使用能够完成任务的任何(完全免费的)技术。我从此线程尝试了解决方案-在Java中,是否有比此代码更优雅的方法将XML文档转换为Java中的String?,他们在哪里

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

但遭到了以下可怕的例外……

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)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

如果您不想依赖某种序列化程序,则最好使用标识转换(en.wikipedia.org/wiki/Identity_transform)。给出的两个答案已经做到了。像这样运行一个空的转换会在后台进行身份转换。
Pavel Veller 2012年

Answers:


88

如果您可以进行转换,则可以尝试此操作。

DocumentBuilderFactory domFact = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = domFact.newDocumentBuilder();
Document doc = builder.parse(st);
DOMSource domSource = new DOMSource(doc);
StringWriter writer = new StringWriter();
StreamResult result = new StreamResult(writer);
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.transform(domSource, result);
System.out.println("XML IN String format is: \n" + writer.toString());

任何想法如何为上述代码编写JUnits?我在编写相同内容时遇到了verifyError。我已经在SO中问了一个问题,如果有空,请回答。stackoverflow.com/q/48560458/5989309
艾伦·帕拉斯

2
我不会为此类代码编写单元测试。您将要测试的框架不仅仅是应用程序逻辑。检查管道“工作”是否可以作为集成或端到端测试的一部分进行。
Adriaan Koster

4
我希望有办法避免这么多样板代码。
bluelurker

我在某些标签(例如“ link”标签)中获得了“ xmlns”属性。<link href =“ my.css” rel =“ stylesheet” type =“ text / css” xmlns =“ w3.org/1999/xhtml ”>。有什么办法可以避免这种情况?
ankur_rajput

90

用一些东西

import java.io.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;

//method to convert Document to String
public String getStringFromDocument(Document doc)
{
    try
    {
       DOMSource domSource = new DOMSource(doc);
       StringWriter writer = new StringWriter();
       StreamResult result = new StreamResult(writer);
       TransformerFactory tf = TransformerFactory.newInstance();
       Transformer transformer = tf.newTransformer();
       transformer.transform(domSource, result);
       return writer.toString();
    }
    catch(TransformerException ex)
    {
       ex.printStackTrace();
       return null;
    }
} 

19
@MubasharAhmadtransformer.setOutputProperty(OutputKeys.INDENT, "yes");
纪尧姆·

10

这对我有用,如本页所记录

TransformerFactory tf = TransformerFactory.newInstance();
Transformer trans = tf.newTransformer();
StringWriter sw = new StringWriter();
trans.transform(new DOMSource(document), new StreamResult(sw));
return sw.toString();

2

基于Zaz答案的Scala版本。

  case class DocumentEx(document: Document) {
    def toXmlString(pretty: Boolean = false):Try[String] = {
      getStringFromDocument(document, pretty)
    }
  }

  implicit def documentToDocumentEx(document: Document):DocumentEx = {
    DocumentEx(document)
  }

  def getStringFromDocument(doc: Document, pretty:Boolean): Try[String] = {
    try
    {
      val domSource= new DOMSource(doc)
      val writer = new StringWriter()
      val result = new StreamResult(writer)
      val tf = TransformerFactory.newInstance()
      val transformer = tf.newTransformer()
      if (pretty)
        transformer.setOutputProperty(OutputKeys.INDENT, "yes")
      transformer.transform(domSource, result)
      Success(writer.toString);
    }
    catch {
      case ex: TransformerException =>
        Failure(ex)
    }
  }

这样,您可以执行任一操作doc.toXmlString()或调用该getStringFromDocument(doc)函数。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.