Solved

How to convert Document to input source

Posted on 2001-07-17
12
719 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: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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

How our DevOps Team Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Nessus Scan 1 71
Vb.net XML Read all nodes and 4 90
Data at the root level is invalid. Line 1, position 1 8 154
XSLT Help 12 33
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, …
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

825 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