XML Editor
Sign up for a WebBoard account Sign Up Keyword Search Search More Options... Options
Chat Rooms Chat Help Help News News Log in to WebBoard Log in Not Logged in
Show tree view Topic
Topic Page 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 Go to previous topicPrev TopicGo to next topicNext Topic
Postnext
amith cnSubject: query on using stylus API for XML/EDI to EDI/XML conversion
Author: amith cn
Date: 06 Jun 2007 06:22 AM
Hi,

I have a query on using stylus API for XML/EDI to EDI/XML conversion

We are trying to do conversion from XML to EDI message using stylus studio APIs

We are using stylus EDI adapter and XSL as transformer.

Stylus will generate an intermediate XML message.
and then XSL will be used for mapping intermediate XML with our custom XML
We have used streams for reading and writing and we donot need any physical files
to read or to write data.


We tried to used a different approach so that we can remove the generation of intermediate
stylus XML


We got the details from
http://www.stylusstudio.com/edi/EDIFACT_translate.html

where it said
For example, to treat C:\edifact-sample.txt as an EDIFACT file, you'd use the URL file:///c:/edifact-sample.txt. But to use it as an XML file, you'd add to the beginning, to get adapter:EDI?file:///c:/edifact-sample.txt.

Using this we can eliminate the generation of intermediate XML explicitly.

But the problem here is, we cannot use streams to read or write.
We need to create a StylusFile With the file path prefixed with adapter:EDI?file:///
where while coversion from XML-EDI we need to simply create a temp file to write EDI data
while coversion from EDI-XML we need to simply create a temp file which has EDI data and then read from it with the path starting with adapter:EDI?file:///

Please let us know is there a way in which we can avoid creating temporary files and avoid intermediate stylus XML and use stream instead


code snippet EDI - XML without explicitly creating an intermediate stylus XML file

StylusFile myStylusFile = StylusFileFactory.getFactory().createStylusFile("adapter:EDI?file:///temp.edi");
StreamSource input = new StreamSource(myStylusFile.getInputStream());

StringWriter aStringWriter = new StringWriter();
StreamResult finalResult = new StreamResult(aStringWriter);

Transformer transformer = TransformerFactory.newInstance().newTransformer(new StreamSource("aa.xsl"));
transformer.transform(input, finalResult);


here we created temp.edi only for reading purpose which is an overhead


code snippet XML- EDI without explicitly creating an intermediate stylus XML file


StreamSource input = new StreamSource(new StringReader((String)pXML));

StylusFile myStylusFile = StylusFileFactory.getFactory().createStylusFile(temp.edi);
StreamResult output = new StreamResult(myStylusFile.getOutputStream());
Transformer transformer = TransformerFactory.newInstance().newTransformer(new StreamSource("aa.xsl"));
transformer.transform(input, output);

here we created an empty temp.edi only for writing the data and then to read the data from it which is an overhead

Postnext
Tony LavinioSubject: query on using stylus API for XML/EDI to EDI/XML conversion
Author: Tony Lavinio
Date: 06 Jun 2007 04:14 PM
We have since improved the API considerably to make what you want
simpler.

I you do the steps in the Pipeline editor, it can generate the
Java code to link the steps together for you.

Or, if you examine the new Converter interface, you'll see that
you can go directly from the output of one step to the input of
another.

See http://www.xmlconverters.com/doc/java/

Postnext
amith cnSubject: query on using stylus API for XML/EDI to EDI/XML conversion
Author: amith cn
Date: 07 Jun 2007 02:33 AM
Hi,

Thank you for your update.

We used the stylus pipeline editor to do the conversion and we got the java code out of it.
But in the code the input and ouput should be the file and we cannot use the streams

We want to use the streams instead as our input will not be a file but it will be string containing EDI message.

We also saw the converter interface example at http://www.xmlconverters.com/doc/java/
Here also if we need to convert directly from EDI to our custom XML
the input source should take the EDI url prefixed with adapter:EDI?

For this we unnecessary need to create a EDI file.
Our application sends the edi in a string format and we are trying use inputstream from that string.

Please let us know how to avoid reading and writing to files and use input and output streams for strings instead without creating intermediate stylus XML.

We have used the stream approach but stylus will generate intermediate stylus xml which we need to avoid.

Please see the code snippet that uses streams but it generates intermediate xml.
Also previous code snipped that was posted didnt generated intermediate stylus xml but it need the file path prefixed adapter:EDI? and we were not able to use streams

XML to EDI conversion

// pXML is our custom input xml in string format
StreamSource input = new StreamSource(new StringReader((String) pXML));

// output. we have used string writer so that output will be written into that
StringWriter aStringWriter = new StringWriter();
StreamResult finalResult = new StreamResult(aStringWriter);

// when we transform the output will be intermediate stylus xml and not edi
Transformer transformer = TransformerFactory.newInstance().newTransformer(new StreamSource("aa.xsl"));
transformer.transform(input, finalResult);

String anIntermediateStylusXML = aStringWriter.toString();

// Now we need to use adapter to get edi
// intermediate stylus xml input
StreamSource binput = new StreamSource(new StringReader((String) anIntermediateStylusXML));

// edi to be generated
ByteArrayOutputStream aByteArrayOutputStream = new ByteArrayOutputStream();
StreamResult bfinalResult = new StreamResult(aByteArrayOutputStream);

ConverterFactory factory = ConverterFactory.newInstance();
Converter fromXML = factory.newConvertFromXML("adapter:EDI");
fromXML.convert(binput, bfinalResult);


String finalEDIMessage = aByteArrayOutputStream.toString();

Postnext
(Deleted User) 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.

Postnext
amith cnSubject: query on using stylus API for XML/EDI to EDI/XML conversion
Author: amith cn
Date: 08 Jun 2007 06:48 AM
Hi,

Thank you for your help.

When i ran the code you pasted, we got nullpointer exception
in the below line.

SAXResult xsltResult = new SAXResult(ediSource.getXMLReader().getContentHandler());


As ediSource is of type SAXSource and it is a new object created and when we do ediSource.getXMLReader() the result will be null and when we do getContentHandler() on null object we get nullpointer exception.

Please let us know what needs to be done. Should XMLReader be set somehow?

Also it will be great help if you let us know optimized approach(without files,without intermediate xml, using only streams) for EDI to XML conversion

Thanks in Advance
Amith

Postnext
(Deleted User) Subject: query on using stylus API for XML/EDI to EDI/XML conversion
Author: (Deleted User)
Date: 08 Jun 2007 09:01 AM
You didn't post your code, so I can only guess what the problem is. Take a look at the code I posted, the call
fromXML.convert(ediSource, finalResult) must appear BEFORE the reference to ediSource.getXMLReader()

The fromXML.convert call initializes the converter, and leaves it idle, waiting for SAX events to arrive. It also creates an XMLReader and puts it in the ediSource object.

Hope this helps
Clyde

Postnext
(Deleted User) Subject: query on using stylus API for XML/EDI to EDI/XML conversion
Author: (Deleted User)
Date: 08 Jun 2007 11:24 AM
If this doesnt solve your problem, please attach you code to an email and send it to stylus-field-report@progress.com

Thanks much,
clyde

Postnext
amith cnSubject: query on using stylus API for XML/EDI to EDI/XML conversion
Author: amith cn
Date: 08 Jun 2007 01:09 PM
Hi Clyde,

Thanks for your updates.

We are not using APIs from com.ddtek.xmlconverter package and we cannot use converter:EDI

We are using stylus APIs from below package
import com.stylusstudio.converter.*;
import com.exln.stylus.io.StylusFileFactory;

and we need to make use of adapter:EDI

We have attached the code as you suggested in the mail sent to stylus-field-report@progress.com

Postnext
amith cnSubject: query on using stylus API for XML/EDI to EDI/XML conversion
Author: amith cn
Date: 11 Jun 2007 07:53 AM

Hi Clyde,

Thank you for your updates.

We are using Stylus studio 2007 with build 735f.
We didn't find any jar that has the com.ddtek.xmlconverter.* files in stylus studio package
May be this version wont support data direct 3.0 xml converters.
Please let us know which version of stylus supports xmlconverters.

The below code sent by you is working fine and we are able to generate EDI output from XML.

It would be of great help if you let us know how to generate XML from EDI as input with the same procedure(no file usage, no intermediate stylus XML generation).


// XML to EDI
ConverterFactory factory = ConverterFactory.newInstance();
Converter fromXML = factory.newConvertFromXML("adapter:EDI");
OutputStream outStream = new ByteArrayOutputStream();
StreamResult finalResult = new StreamResult(outStream);
SAXSource ediSource = new SAXSource();
SAXConversion conversion = (SAXConversion)fromXML.convert(ediSource, finalResult);
SAXResult xsltResult = new SAXResult(conversion);
StreamSource input = new StreamSource(new StringReader((String) pXML));
transformUsingXSL(input, xsltResult);
System.out.println("outStream.toString() "+outStream.toString());



Thanks in advance

Amith

Postnext
(Deleted User) Subject: query on using stylus API for XML/EDI to EDI/XML conversion
Author: (Deleted User)
Date: 11 Jun 2007 01:59 PM
The version you have now supports the old API: com.stylusstudio.converter.

XMLConverters 3.0 and Stylus Studio 2007 release 2 support both the old API: com.stylusstudio.converter (deprecated) and the new API: com.ddtek.xmlconverters.

The next version (I don’t know what it will be named) will probably drop the old API and contain only the com.ddtek.xmlconverters API.

If you want to convert EDI to XML, and send that directly to an XSLT, then SAX is the way to go.

Using the old API, refer to the attached demo.java (which is from the examples/Adapters directory of your StylusStudio installation). Look at example 4.

Using the new API, refer to the attached newdemo.java, example 4.

Hope this helps,

clyde


Unknownjava.demo
demo code using the old api

Unknownnewdemo.java
demo code using the new api

Postnext
amith cnSubject: query on using stylus API for XML/EDI to EDI/XML conversion
Author: amith cn
Date: 12 Jun 2007 02:15 PM
Hi Clyde,

Now we are also able to generate XML from EDI.
We are still using old stylus APIs.

Once we get new version installed, we will use new APIs for the same.

Thanks for your wonderful support in solving the issue.

Thanks,
Amith

Posttop
amith cnSubject: query on using stylus API for XML/EDI to EDI/XML conversion
Author: amith cn
Date: 13 Jun 2007 02:20 PM
Hi Clyde,

Now we have been shifted to newer version of stylus studo release 2.
We are making use of com.ddtek.xmlconverter APIs.

When we try to open any EDI file using stylus UI, we will get error as below
ERROR: 'com.sun.org.apache.xml.internal.utils.WrappedRuntimeException: Error during conversion: Index: 4, Size: 4'
Exception in thread "main" javax.xml.transform.TransformerException: javax.xml.transform.TransformerException:

But it was not happening in the previous version and we were able to open the EDI
----------------------------------------------------------------------
Also we are trying to convert from custom XML to EDI in java.
Here our custom xml pXML will have our own tags(no <UNB> or <UNH> tags) and we will use aaa.xsl for mapping purpose.

aaa.xsl will have mapping something like and so on
<UNH01><xsl:value-of select="p1:Paoreq/MessageHeaderSegment/MessageRefNumber"/>
</UNH01>

When we run the below code we will get an exception always. May be while transforming it is looking in the custom XML for UNH segment and it is not founding, But our custom XML will have other segment and XSL will map it.

Please let us know about this issue.

ConverterFactory factory = new ConverterFactory();
OutputStream outStream = new ByteArrayOutputStream();
StreamResult converterResult = new StreamResult(outStream);
SAXSource converterSource = new SAXSource();
Converter fromXml = factory.newConvertFromXML("converter:EDI");
fromXml.convert(converterSource, converterResult);
SAXResult xsltResult = new SAXResult();
xsltResult.setHandler(converterSource.getXMLReader().getContentHandler());
StreamSource xsltSource = new StreamSource("aaa.xsl");
StreamSource xsltInput = new StreamSource(new StringReader((String) pXML));
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer xslt = transformerFactory.newTransformer(xsltSource);
xslt.transform(xsltInput, xsltResult);
System.out.println("output string for new version of stylus is "+outStream.toString());

-------------------------------------------------------------------

ERROR: 'Error during conversion: $.. Segment UNH: Segment not expected for this message (0)'
Exception in thread "main" javax.xml.transform.TransformerException: org.xml.sax.SAXException: Error during conversion: $.. Segment UNH: Segment not expected for this message (0)
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:650)
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:279)
at client4.convertsendingmessage(client4.java:99)
at client4.main(client4.java:52)

 
Topic Page 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 Go to previous topicPrev TopicGo to next topicNext Topic
Download A Free Trial of Stylus Studio 6 XML Professional Edition Today! Powered by Stylus Studio, the world's leading XML IDE for XML, XSLT, XQuery, XML Schema, DTD, XPath, WSDL, XHTML, SQL/XML, and XML Mapping!  
go

Log In Options

Site Map | Privacy Policy | Terms of Use | Trademarks
Stylus Scoop XML Newsletter:
W3C Member
Stylus Studio® and DataDirect XQuery ™are from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2016 All Rights Reserved.