|
next
|
 Subject: query on using stylus API for XML/EDI to EDI/XML conversion Author: (Deleted User) Date: 07 Jun 2007 01:00 PM
|
Hi,
I understand you have 2 issues
1) how to connect the xslt directly to our EDI converter without any temporary file or String
2) how to get the output of the EDI without a temporary file or String.
You will notice that the most efficient way to connect an xslt to a fromXML converter is using a SAX ContentHandler. To do this, you have to “start” the convreter first, and get its ContentHandler. Then you start the xslt.
This example shows how to do both 1) and 2), with the output from the EDI available as an InputStream
// setup the EDI converter
ConverterFactory factory = new ConverterFactory();
Converter fromXML = factory.newConvertFromXML("converter:EDI");
InputStreamResult finalResult = new InputStreamResult();
SAXSource ediSource = new SAXSource();
fromXML.convert(ediSource, finalResult);
// the converter is now waiting for SAX events to arrive.
// setup the transformer to send SAX events to the converter
SAXResult xsltResult = new SAXResult(ediSource.getXMLReader().getContentHandler());
String pXML = …
StreamSource input = new StreamSource(new StringReader((String) pXML));
Transformer transformer = TransformerFactory.newInstance().newTransformer(new StreamSource("aa.xsl"));
transformer.transform(input, xsltResult);
// now read the result from the input stream
InputStream resultStream = finalResult.getInputStream();
byte[] buffer = new byte[1000];
int ret;
while ((ret=resultStream.read(buffer)) > 0)
System.out.println(new String(buffer, 0, ret));
// after consuming the result data, the consumer should close the InputStream
resultStream.close();
This examples shows how to do both 1) and 2), with the output from the EDI written to an OutputStream
// setup the EDI converter
ConverterFactory factory = new ConverterFactory();
Converter fromXML = factory.newConvertFromXML("converter:EDI");
OutputStream outStream = …
StreamResult finalResult = new StreamResult(outStream);
SAXSource ediSource = new SAXSource();
fromXML.convert(ediSource, finalResult);
// the converter is now waiting for SAX events to arrive.
// setup the transformer to send SAX events to the converter
SAXResult xsltResult = new SAXResult(ediSource.getXMLReader().getContentHandler());
String pXML = …
StreamSource input = new StreamSource(new StringReader((String) pXML));
Transformer transformer = TransformerFactory.newInstance().newTransformer(new StreamSource("aa.xsl"));
transformer.transform(input, xsltResult);
// the output has now been written to the OutputStream, which must be closed, either in this program or somewhere else.
outStream.close();
I hope this helps,
Clyde Kessel
XMLConverters development team.
|
|
|
|