Solved

How to convert Document to input source

Posted on 2001-07-17
12
717 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
 
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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 to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

The Problem How to write an Xquery that works like a SQL outer join, providing placeholders for absent data on the outer side?  I give a bit more background at the end. The situation expressed as relational data Let’s work through this.  I’ve …
The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

706 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now