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

x
?
Solved

How to convert Document to input source

Posted on 2001-07-17
12
Medium Priority
?
730 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: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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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…
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…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

705 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