Solved

How to convert Document to input source

Posted on 2001-07-17
12
721 Views
Last Modified: 2008-01-16
How can I convert my Document into a StreamSource?
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
  • 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:wbradney
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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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:wbradney
ID: 6291121
0
 
LVL 6

Expert Comment

by:wbradney
ID: 6291123
0
 
LVL 6

Expert Comment

by:wbradney
ID: 6291126
Hello?
0
 
LVL 6

Accepted Solution

by:
wbradney earned 50 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:wbradney
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:wbradney
ID: 6299525
jerelw,

org.apache.xerces.parsers.DOMParser

Regards,
WMB
0

Featured Post

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

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

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

730 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