Efficient way to convert DOM to an OutputStream or String

Posted on 2001-07-16
Medium Priority
Last Modified: 2007-11-27
Does anyone know how to convert a large Document into a String or OutputStream?

public static final String getXML(Document document) throws Exception {
       try {
       OutputFormat  format   = new OutputFormat(document);  
       StringWriter stringOut = new StringWriter();      
       XMLSerializer  serial  = new XMLSerializer( stringOut, format );  
       return stringOut.toString();
     catch(Exception e) {
       throw new Exception("XML to String Err: " + e);

I am using the latest xerces.jar

This blows up at around 30000 records.

Question by:jerelw
  • 3

Author Comment

ID: 6287835
Exact Error:

     at java.lang.StringBuffer.expandCapacity(StringBuffer.java:202)
     at java.lang.StringBuffer.append(StringBuffer.java:455)
     at java.io.StringWriter.write(StringWriter.java:86)
     at java.io.Writer.write(Writer.java:107)
     at org.apache.xml.serialize.Printer.printText(Printer.java:258)
     at org.apache.xml.serialize.BaseMarkupSerializer.content(BaseMarkupSerializer.java:1044)
     at org.apache.xml.serialize.BaseMarkupSerializer.characters(BaseMarkupSerializer.java:1070)
     at org.apache.xml.serialize.BaseMarkupSerializer.serializeNode(BaseMarkupSerializer.java:900)
     at org.apache.xml.serialize.XMLSerializer.serializeElement(XMLSerializer.java:639)
     at org.apache.xml.serialize.BaseMarkupSerializer.serializeNode(BaseMarkupSerializer.java:945)
     at org.apache.xml.serialize.XMLSerializer.serializeElement(XMLSerializer.java:639)
     at org.apache.xml.serialize.BaseMarkupSerializer.serializeNode(BaseMarkupSerializer.java:945)
     at org.apache.xml.serialize.XMLSerializer.serializeElement(XMLSerializer.java:639)
     at org.apache.xml.serialize.BaseMarkupSerializer.serializeNode(BaseMarkupSerializer.java:945)
     at org.apache.xml.serialize.BaseMarkupSerializer.serialize(BaseMarkupSerializer.java:405)
     at com.am2.data.JDBCWrap.getResultSet(JDBCWrap.java:423)
     at com.am2.data.JDBCWrap.getResultSetXML(JDBCWrap.java:346)
     at com.am2.data.JDBCWrap.getResultSetXML(JDBCWrap.java:337)
     at com.am2.data.search.DataSearch.getResultSetXML(DataSearch.java:40)
     at com.am2.server.Test.process(Test.java:184)
     at com.am2.server.Module.run(Module.java:133)
     at Am2Web.doProcess(Am2Web.java:78)
     at Am2Web.doGet(Am2Web.java:33)
     at javax.servlet.http.HttpServlet.service(HttpServlet.java:715)
     at javax.servlet.http.HttpServlet.service(HttpServlet.java:840)
     at com.sun.web.core.ServletWrapper.handleRequest(ServletWrapper.java:155)
     at com.sun.web.core.InvokerServlet.service(InvokerServlet.java:168)
     at javax.servlet.http.HttpServlet.service(HttpServlet.java:840)
     at com.sun.web.core.ServletWrapper.handleRequest(ServletWrapper.java:155)
     at com.sun.web.core.Context.handleRequest(Context.java:414)
     at com.sun.web.server.ConnectionHandler.run(ConnectionHandler.java:139)


Expert Comment

by:Igor Bazarny
ID: 6288071

Do you really need an in-memory String representation? Can you turn your public static final String getXML(Document document) throws Exception
into public static final void getXML(Document document, Writer consumer) throws Exception? Than you can redirect output to consumer, handle it (write to servlet out stream) and decrease memory requirements.

BTW, few style notes:
- static methods are final by default--there is no way to override static method.
- your method of exception handling hides stack trace of nested exception. I believe, there is no need to catch Exception if you intend to throw same or less specific exception type. If you need to change exception type, take care that exception you throw contains nested exception and adds nested exception stack trace to newly throws exception stack trace.  

Igor Bazarny

Accepted Solution

rakeshshringi earned 200 total points
ID: 6289677
For me it works fine:

public static String domToStr(Document doc) {

    long time1 = System.currentTimeMillis();

    OutputFormat format = new OutputFormat(doc);    // Serialize DOM
    StringWriter stringOut = new StringWriter();    // Writer will be a String

    try {
      XMLSerializer serial = new XMLSerializer(stringOut, format);
      serial.asDOMSerializer();    // As a DOM Serializer
    catch (Exception e) {

    long time2 = System.currentTimeMillis();
    Utils.timeLog("domToStr", time1, time2);

//    return normalize(stringOut.toString());    // Spit out DOM as a normalized String
    return stringOut.toString();    // Spit out DOM as a String


Author Comment

ID: 6289823
For me it works fine:

It works for me, too.

When I generate a Document of over 30000 records, however, I run out of memory...

Is there another way to convert the Document to a String more efficiently?

Author Comment

ID: 6290066
you're right rakeshshringi, but how do I convert my Document into an InputSource so I can resolve it against an XSL?

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month5 days, 16 hours left to enroll

589 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question