Go Premium for a chance to win a PS4. Enter to Win


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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

824 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