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

后端 未结 4 745
我在风中等你
我在风中等你 2020-12-02 16:41

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 so

相关标签:
4条回答
  • 2020-12-02 16:47

    If you are ok to do transformation, you may try this.

    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());
    
    0 讨论(0)
  • 2020-12-02 16:54

    This worked for me, as documented on this page:

    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer trans = tf.newTransformer();
    StringWriter sw = new StringWriter();
    trans.transform(new DOMSource(document), new StreamResult(sw));
    return sw.toString();
    
    0 讨论(0)
  • 2020-12-02 17:00

    A Scala version based on Zaz's answer.

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

    With that, you can do either doc.toXmlString() or call the getStringFromDocument(doc) function.

    0 讨论(0)
  • 2020-12-02 17:07

    use some thing like

    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;
        }
    } 
    
    0 讨论(0)
提交回复
热议问题