Discussions

XML & Web services: Convert Document object to String object

  1. Convert Document object to String object (3 messages)

    I want to parse the XML file, and output the string object of XML representation.
    I tried to convert Document object to String object, but it is unsuccessful. Anyone knows
    what I miss?

    Here's the output of this program, which is wrong:
    [person: null]


    import java.io.*;
    import javax.xml.parsers.*;
    import org.w3c.dom.*;
    import org.xml.sax.*;

    /** transform Document object to XML string */
    public class ParserTest
    {
      public static void main(String[] args) throws ParserConfigurationException, SAXException
      {
    try
    {
    String xmlFile = "mydoc.xml";
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document doc = builder.parse(new File(xmlFile));
    Element root = doc.getDocumentElement();
    String s = root.toString();
    System.out.println(s);
    }
    catch(IOException e)
    { e.printStackTrace();
    }
        }
    }

    //mydoc.xml
    <?xml version = "1.0"?>
    <person>
    <name>Joe</name>
    <age>20</age>
    </person>
  2. Matt,
    Here is the method that converts the Document to String equivalent

    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;
        }
    }
  3. Thanks Ravi Sri[ Go to top ]

    Hi Ravi Sri,

    Thanks a ton for your smart code suggestion to Matt, this piece of code helped me to go home after a hectic java to xml convertion project.

    Best regards
    Nagesh
    nageshbab@rediffmail.com
  4. Hi,

     

    It is good logic whatever you are using. made some chages to your code .now it is working fine. find the working code.

     

     

    package com.test;

    import java.io.File;
    import java.io.IOException;

    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;

    import org.w3c.dom.Document;
    import org.w3c.dom.Element;


    public class Sample {
     public static void main(String[] args)
       {
     try
     {
      
     String xmlFile = "src/com/test/Sample.xml";
     DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
     DocumentBuilder builder = factory.newDocumentBuilder();
     Document doc = builder.parse(new File(xmlFile));
     Element root = doc.getDocumentElement();
     String s = root.toString();
     System.out.println(s);
     }
     catch(Exception e)
     { e.printStackTrace();
     }
     
       }
     

    }