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

x
?
Solved

How to convert Document to input source

Posted on 2001-07-17
12
Medium Priority
?
732 Views
Last Modified: 2008-01-16
How can I convert my Document into a StreamSource?
0
Comment
Question by:jerelw
  • 7
  • 4
12 Comments
 
LVL 8

Expert Comment

by:Dave_Greene
ID: 6290205
You can convert it into a string then you can pass the string to whatever

strXML = oDOM.xml
0
 
LVL 3

Author Comment

by:jerelw
ID: 6290394
I am generating my XML by building a DOM.

After appending my root Node to my Document, I want to resolve the Document against an XSL.

Converting the XML to a String blows up my memory since it is over 300000 records.

0
 
LVL 6

Expert Comment

by:Wayne Bradney
ID: 6290560
jerelw,

I assume you're using Apache Xerces and Xalan? Use XMLSerializer to serialize a document to a piped output stream, something like:

PipedInputStream in;
PipedOutputStream out;
Document doc;
Transformer tran;
StreamResult res;
...
XMLSerializer ser = new XMLSerializer(out, new OutputFormat("xml", "UTF-8", false));
ser.serialize(doc);
tran.transform(in, res);

If you want to convert big XML to a string, but you're running out of memory, you could use -Xmx and -Xms switches to your JVM to increase memory allocation.

Regards,
WMB
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Author Comment

by:jerelw
ID: 6291011
How can I pass in the XMLSerializer Object to a method like this?

xml and xsl are StringReader Objects...

TransformerFactory tFactory = TransformerFactory.newInstance();        
      Transformer transformer     = tFactory.newTransformer(new StreamSource(xsl));        
       ByteArrayOutputStream baos  = new ByteArrayOutputStream();    
       transformer.transform(new StreamSource(xml), new StreamResult(baos));      
       return baos.toString();


This works fine for small documents, but I have a Document that is over 300000 Elements.

I run out of memory on the conversion to a String using this code:

OutputFormat  format   = new OutputFormat(document);  
       StringWriter stringOut = new StringWriter();      
       XMLSerializer  serial  = new XMLSerializer( stringOut, format );  
       serial.asDOMSerializer();                
       serial.serialize(document.getDocumentElement());
       return stringOut.toString();

...but if I already have my Document(root Element) how can I bypass the String - StringReader conversion and pass the Document to my processor as a Node?


0
 
LVL 3

Author Comment

by:jerelw
ID: 6291109
how about converting a Document to a URI for a SAXWriter?

Does anyone know how to make this conversion?
0
 
LVL 6

Expert Comment

by:Wayne Bradney
ID: 6291121
0
 
LVL 6

Expert Comment

by:Wayne Bradney
ID: 6291123
0
 
LVL 6

Expert Comment

by:Wayne Bradney
ID: 6291126
Hello?
0
 
LVL 6

Accepted Solution

by:
Wayne Bradney earned 200 total points
ID: 6291129
jerelw,

If you want to do this without writing the whole document to a file or an in-memory buffer you should use a pipe. I've dug up an example I used a while back and updated it to work with Xalan2:

import java.io.*;
import javax.xml.transform.*;
import javax.xml.transform.stream.*;

import org.w3c.dom.*;
import org.apache.xerces.parsers.*;
import org.apache.xml.serialize.*;
import org.xml.sax.*;

public class StreamedTransform
{
  public static void main(String[] args)
  {
    System.out.println("Checking args...");
    if (args.length < 2)
    {
      System.out.println("usage: Transform <input_doc> <xsl_doc> [<output_doc>]");
      System.exit(0);
    }

    try
    {
      PipeTransformer pt = new PipeTransformer(null, args[1], (args.length == 2 ? null : args[2]));
      PipeSerializer ps = new PipeSerializer(pt.getInPipe(), args[0]);

      pt.start();
      ps.start();
    }
    catch (Exception e)
    {
      e.printStackTrace();
    }
  }

  private static class PipeSerializer extends Thread
  {
    String xmlFile;
    PipedOutputStream outPipe;
    PipedInputStream inPipe;

    public PipeSerializer(PipedInputStream inPipe, String xmlFile)
    {
      try
      {
        this.xmlFile = xmlFile;
        this.inPipe = inPipe;
        if (inPipe == null)
        {
          outPipe = new PipedOutputStream();
        }
        else
        {
          outPipe = new PipedOutputStream(inPipe);
        }
      }
      catch (Exception e)
      {
        e.printStackTrace();
      }
    }

    public PipedOutputStream getOutPipe()
    {
      return outPipe;
    }

    public void run()
    {
      System.out.println("starting serialize thread...");
      try
      {
        DOMParser parser = new DOMParser();
        parser.parse(new InputSource(xmlFile));
        Document doc = parser.getDocument();
        XMLSerializer ser = new XMLSerializer(outPipe, new OutputFormat("xml", "UTF-8", false));
        ser.serialize(doc);
        System.out.println("serialize done.");
        outPipe.close();
      }
      catch (Exception e)
      {
        e.printStackTrace();
      }
    }
  }

  private static class PipeTransformer extends Thread
  {
    PipedOutputStream outPipe;
    PipedInputStream inPipe;
    String xslFile;
    String outFile;
    Transformer transformer;

    public PipeTransformer(PipedOutputStream outPipe, String xslFile, String outFile)
    {
      try
      {
        this.outPipe = outPipe;
        this.xslFile = xslFile;
        this.outFile = outFile;
        if (outPipe == null)
        {
          inPipe = new PipedInputStream();
        }
        else
        {
          inPipe = new PipedInputStream(outPipe);
        }
        TransformerFactory tFactory = TransformerFactory.newInstance();
        transformer = tFactory.newTransformer(new StreamSource(xslFile));
      }
      catch (Exception e)
      {
        e.printStackTrace();
      }
    }

    public PipedInputStream getInPipe()
    {
      return inPipe;
    }

    public void run()
    {
      System.out.println("starting transform thread...");
      try
      {
        OutputStream out;

        if (outFile == null)
        {
          out = System.out;
        }
        else
        {
          out = new FileOutputStream(outFile);
        }

        transformer.transform(new StreamSource(inPipe), new StreamResult(out));
        System.out.println("transform done.");
        inPipe.close();
      }
      catch (Exception e)
      {
        e.printStackTrace();
      }
    }
  }
}


Hope this helps.

Regards,
WMB
0
 
LVL 6

Expert Comment

by:Wayne Bradney
ID: 6291130
(Don't know what happen the first two attempts!)
0
 
LVL 3

Author Comment

by:jerelw
ID: 6299487
I am getting a compilation error.

Which DOMParser are you referring to:

dom.wrappers.DOMParser?

0
 
LVL 6

Expert Comment

by:Wayne Bradney
ID: 6299525
jerelw,

org.apache.xerces.parsers.DOMParser

Regards,
WMB
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
Create a Windows 10 custom Image with custom task bar and custom start menu using XML for deployment.
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…

782 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