Pages

Thursday, July 03, 2008

Convert XML file to XML String in java

I had to find out a way to read an XML file, and convert it to java String. Below is the code snippet I used.

public String convertXMLFileToString(String fileName)
        {
          try{
            DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
            InputStream inputStream = new FileInputStream(new File(fileName));
            org.w3c.dom.Document doc = documentBuilderFactory.newDocumentBuilder().parse(inputStream);
            StringWriter stw = new StringWriter();
            Transformer serializer = TransformerFactory.newInstance().newTransformer();
            serializer.transform(new DOMSource(doc), new StreamResult(stw));
            return stw.toString();
          }
          catch (Exception e) {
            e.printStackTrace();
          }
            return null;
        }

22 comments:

  1. Anonymous12:43 PM

    You code is really helpful to me. Thank you very much.
    Suresh

    ReplyDelete
  2. Hai what if i want to convert xml string to xml file

    ReplyDelete
  3. just write it to file with .xml extension.. if you want to format it, then am not sure hw to do it..

    ReplyDelete
  4. Anonymous6:34 PM

    thanks
    it helped me a lot

    ReplyDelete
  5. Anonymous8:40 AM

    Thanks for the posting.. Sun should just make a simple class to do this.

    ReplyDelete
  6. Anonymous1:18 AM

    Thanks a lot

    ReplyDelete
  7. Anonymous6:44 AM

    Hi nice code......
    It helped me

    ReplyDelete
  8. Anonymous12:26 PM

    Straight and to the point. Thanks for sharing

    ReplyDelete
  9. Jayanth2:51 PM

    Thank you so much. This is just awesome.

    ReplyDelete
  10. Anonymous1:19 AM

    And this can simply be put in any class and it will function properly?

    ReplyDelete
  11. Anonymous4:16 PM

    Thank you so much!! :)

    ReplyDelete
  12. Anonymous7:14 AM

    FANTASTIC!! Thank you!

    ReplyDelete
  13. Anonymous1:44 AM

    Yaaa, Nice Man thanks a lot - Ramas

    ReplyDelete
  14. I get this error below, can you please help.

    Exception in thread "main" java.lang.AbstractMethodError: org.apache.xerces.dom.DeferredDocumentImpl.getXmlStandalone()Z
    at com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.setDocumentInfo(DOM2TO.java:373)
    at com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.parse(DOM2TO.java:127)
    at com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.parse(DOM2TO.java:94)
    at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transformIdentity(TransformerImpl.java:661)
    at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:707)
    at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:313)
    at

    ReplyDelete
  15. Anonymous2:29 AM

    Very helpfull code! Thank you soo much!

    ReplyDelete
  16. Anonymous2:12 AM

    Great work! This code is very helpful!! Thank U!

    ReplyDelete
  17. Anonymous4:50 AM

    Very helpfull..Thank you very much!

    ReplyDelete
  18. Anonymous3:24 AM

    good

    ReplyDelete
  19. Anonymous12:11 PM

    very helpful. thanks

    ReplyDelete
  20. Anonymous10:50 PM

    thanks a lot dude.....

    ReplyDelete
  21. thanks very much. this helped just in time.

    ReplyDelete