Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Efficient way to convert DOM to an OutputStream or String

Posted on 2001-07-16
5
Medium Priority
?
361 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 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
      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

The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
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:
Suggested Courses

688 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