Solved

Efficient way to convert DOM to an OutputStream or String

Posted on 2001-07-16
5
334 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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 Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
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…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

947 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

21 Experts available now in Live!

Get 1:1 Help Now