Solved

Efficient way to convert DOM to an OutputStream or String

Posted on 2001-07-16
5
330 Views
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 );  
       serial.asDOMSerializer();                
       serial.serialize(document.getDocumentElement());
       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.

0
Comment
Question by:jerelw
  • 3
5 Comments
 
LVL 3

Author Comment

by:jerelw
ID: 6287835
Exact Error:

java.lang.OutOfMemoryError:
     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)

0
 
LVL 7

Expert Comment

by:Igor Bazarny
ID: 6288071
Hi,

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.  

Regards,
Igor Bazarny
0
 

Accepted Solution

by:
rakeshshringi earned 50 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
      serial.serialize(doc.getDocumentElement());
    }
    catch (Exception e) {
      cat.error(e.toString());
    }

    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

  }
0
 
LVL 3

Author Comment

by:jerelw
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?
0
 
LVL 3

Author Comment

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

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

743 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now