EDI file generation using SMOOKS - java

How can I convert XML file to EDI file using SMOOKS?
I am able to convert EDI to XML, in fact this is part of examples provided by SMOOKS.

Based on your question i tried to do some research on it . Please check if it helps you .
So here's the source edi file that is to be transformed:
HDR*1*0*59.97*64.92*4.95*Wed Nov 15 13:45:28 EST 2006
CUS*user1*Harry^Fletcher*SD
ORD*1*1*364*The 40-Year-Old Virgin*29.98
ORD*2*1*299*Pulp Fiction*29.99
And this is the expected result of our transformation:
<Order>
<header>
<order-id>1</order-id>
<status-code>0</status-code>
<net-amount>59.97</net-amount>
<total-amount>64.92</total-amount>
<tax>4.95</tax>
<date>Wed Nov 15 13:45:28 EST 2006</date>
</header>
<customer-details>
<username>user1</username>
<name>
<firstname>Harry</firstname>
<lastname>Fletcher</lastname>
</name>
<state>SD</state>
</customer-details>
<order-item>
<position>1</position>
<quantity>1</quantity>
<product-id>364</product-id>
<title>The 40-Year-Old Virgin</title>
<price>29.98</price>
</order-item>
<order-item>
<position>2</position>
<quantity>1</quantity>
<product-id>299</product-id>
<title>Pulp Fiction</title>
<price>29.99</price>
</order-item>
</Order>
The Smooks Configuration
We simply specify the SmooksEDIParser  as the stream parser. More transformation configurations could be added to transform this message further.
Here's the configuration ("smooks-config.xml"):
<?xml version="1.0"?>
<smooks-resource-list xmlns="http://www.milyn.org/xsd/smooks-1.1.xsd"
xmlns:edi="http://www.milyn.org/xsd/smooks/edi-1.1.xsd">
<!--
Configure the EDI Reader to process the message stream into a stream of SAX events.
-->
<edi:reader mappingModel="/example/edi-to-xml-order-mapping.xml" />
</smooks-resource-list>
Here's the edi mapping ("/src/main/java/example/edi-to-xml-order-mapping.xml"):
<?xml version="1.0" encoding="UTF-8"?>
<medi:edimap xmlns:medi="http://www.milyn.org/schema/edi-message-mapping-1.0.xsd">
<medi:description name="DVD Order" version="1.0" />
<medi:delimiters segment="
" field="*" component="^" sub-component="~" />
<medi:segments xmltag="Order">
<medi:segment segcode="HDR" xmltag="header">
<medi:field xmltag="order-id" />
<medi:field xmltag="status-code" />
<medi:field xmltag="net-amount" />
<medi:field xmltag="total-amount" />
<medi:field xmltag="tax" />
<medi:field xmltag="date" />
</medi:segment>
<medi:segment segcode="CUS" xmltag="customer-details">
<medi:field xmltag="username" />
<medi:field xmltag="name">
<medi:component xmltag="firstname" />
<medi:component xmltag="lastname" />
</medi:field>
<medi:field xmltag="state" />
</medi:segment>
<medi:segment segcode="ORD" xmltag="order-item" maxOccurs="-1">
<medi:field xmltag="position" />
<medi:field xmltag="quantity" />
<medi:field xmltag="product-id" />
<medi:field xmltag="title" />
<medi:field xmltag="price" />
</medi:segment>
</medi:segments>
</medi:edimap>
Executing The Transformation:
// Instantiate Smooks with the config...
Smooks smooks = new Smooks("smooks-config.xml");
try {
// Filter the input message to the outputWriter...
smooks.filterSource(new StreamSource(messageIn), new
StreamResult(messageOut));
} finally {
smooks.close();
}

Related

Axis soap logging issue

I am trying to print Axis Request and response logs(with current date appended) , in order to achieve the same I have created client-config.wsdd but on each web service call a new log file is getting generated resulting is large log file name.
<?xml version="1.0" encoding="UTF-8"?>
<deployment xmlns="http://xml.apache.org/axis/wsdd/" xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">
<handler name="log" type="java:org.apache.axis.handlers.LogHandler">
<parameter name="LogHandler.fileName" value="c:/logs/axis.log" />
</handler>
<globalConfiguration>
<requestFlow>
<handler type="log" />
</requestFlow>
<responseFlow>
<handler type="log" />
</responseFlow>
</globalConfiguration>
<transport name="http" pivot="java:org.apache.axis.transport.http.HTTPSender" />
</deployment>
Any suggestion would be highly appreciated.

Add an node to an xml when the same node is present in the xml as an empty tag

<?xml version="1.0" encoding="UTF-8"?>
<RESPONSE>
<STATUS>
<SERVERTIME><![CDATA[15319123123262]]></SERVERTIME>
<TYPE><![CDATA[getData]]></TYPE>
<RESPONSE_VERSION><![CDATA[1]]></RESPONSE_VERSION>
<DATA_VERSION><![CDATA[1]]></DATA_VERSION>
<VALUE><![CDATA[1]]></VALUE>
<ERRORS />
<WARNINGS />
</STATUS>
<DATA>
<CONTENT RECORDID="f2e110aa8ca24aewq929d26b9fcf3108962">
<METARESOURCEPATH><![CDATA[sites/xyz/content/meta/INFORMATION/0/INF427/]]></METARESOURCEPATH>
<REVIEW_TIMESTAMP><![CDATA[2018-07-20 15:36:00 IST]]></REVIEW_TIMESTAMP>
<REVIEW_TIMESTAMP_MILLIS><![CDATA[1532081160000]]></REVIEW_TIMESTAMP_MILLIS>
<VIEWS>
<VIEW>
<NAME><![CDATA[xyz]]></NAME>
<REFERENCE_KEY><![CDATA[xyz]]></REFERENCE_KEY>
<GUID><![CDATA[6d024478feb441231661f163de62d6e]]></GUID>
<OBJECTID><![CDATA[001]]></OBJECTID>
<PARENTID />
<CHILDCOUNT><![CDATA[8]]></CHILDCOUNT>
</VIEW>
</VIEWS>
<CATEGORIES />
</CONTENT>
</DATA>
</RESPONSE>
I need to add an element to CATEGORIES WHICH IS OF THE FORMAT.
When ever i try to add a new category it does not accept it because it finds a duplicate categories tag. But when i try to remove that empty tag , that is also not accepted because getelementbyname only returns null for categories.
<CATEGORIES>
<CATEGORY>
<NAME><![CDATA[xyz]]></NAME>
<REFERENCE_KEY><![CDATA[xyz]]></REFERENCE_KEY>
<GUID><![CDATA[6d024478feb441231661f163de62d6e]]></GUID>
<OBJECTID><![CDATA[001]]></OBJECTID>
<PARENTID />
<CHILDCOUNT><![CDATA[8]]></CHILDCOUNT>
</CATEGORY>
</CATEGORIES>
How can this be done with max efficiency ?

SimpleFramework Xml parsen

We have to Parse a XML String
<GetTimetableEntriesResponse Result="Success">
<TimetableEntries>
<TimetableEntry xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" order="1" TimetableEntryId="7645" TimetableEntryGroupId="1739" Name="Alm_04.jpg" Duration="7000">
<Template xsi:type="XmlImageTemplate" duration="7000">
<VerticalScroll>false</VerticalScroll>
<HorizontalScroll>false</HorizontalScroll>
<ScrollSpeed>50</ScrollSpeed>
<ScrollSpeedBack>100</ScrollSpeedBack>
<ScrollStartDelay>0</ScrollStartDelay>
<MediaItem id="6629" x="0" y="0" zIndex="0" type="jpg">
<width xsi:nil="true" />
<height xsi:nil="true" />
<rotation xsi:nil="true" />
<rotationcenterx xsi:nil="true" />
<rotationcentery xsi:nil="true" />
<alpha xsi:nil="true" />
<VerticalScroll xsi:nil="true" />
<HorizontalScroll xsi:nil="true" />
<ScrollSpeed xsi:nil="true" />
<ScrollSpeedBack xsi:nil="true" />
<ScrollStartDelay xsi:nil="true" />
<Loop xsi:nil="true" />
</MediaItem>
<Effect xsi:type="XmlFade" start="0" from="0" to="1" duration="500" />
<Effect xsi:type="XmlFade" start="6500" from="1" to="0" duration="500" />
<TemplateSource xsi:nil="true" />
</Template>
</TimetableEntry>
</GetTimetableEntriesResponse>
but <TimetableEntry> got no <Template> directly in it, programmatically it is handled in a Subclass called <TimetableData> which is in <TimetableEntry>
That means we handle it in java like :
<TimetableEntry>
<TimetableData>
<Template></Template>
</TimetableData>
</TimetableEntry>
We don't know how we should parse it in our Objects.

Completly new to XML and JAXB. How do I go about unmarshalling this snippet?

This is a sample of the XML I've got. It contains several more rows with item
<items>
<item name="Alfa Romeo">
<models>
<model to="2001" since="1994" name="145/146" />
<model since="2001" name="147" />
<model to="1997" since="1992" name="155" />
<model since="1997" name="156" />
<model since="2007" name="159" />
<model to="1998" since="1990" name="164" />
<model since="1999" name="166" />
<model since="2004" name="GT" />
<model since="1996" name="GTV" />
<model to="2007" since="1996" name="Spider" />
<model since="2007" name="Spider" />
</models>
</item>
<item name="Fiat">
<models>
<model since="1995" name="Barchetta" />
<model since="2007" name="Bravo" />
<model since="1995" name="Bravo and Brava" />
<model to="1998" since="1993" name="Cinquecento" />
<model to="2000" since="1995" name="Coupe" />
<model since="2001" name="Doblo" />
<model since="2007" name="Grande Punto" />
<model to="2007" since="2004" name="Idea" />
<model to="2001" since="1997" name="Marea" />
<model since="1999" name="Multipla" />
<model since="2004" name="Panda" />
<model to="2003" since="1994" name="Punto" />
<model since="2003" name="Punto" />
<model to="2004" since="1998" name="Seicento" />
<model since="2002" name="Stilo" />
<model to="1996" since="1991" name="Tempra" />
<model to="1996" since="1989" name="Tipo" />
<model to="2003" since="1995" name="Ulysse" />
</models>
</item>
</items>
Is it posible to unmarshall this into objects of Model with fields private int to; private int since; private String name; contained in an ArrayList inside models of Car(item) contained in an ArrayList?
Alternatively with objects of Car with fields private String brand(name); private int since; private int to; private String model(name) contained in an ArrayList?
If so how? or if not how other way can I retrieve the information into objects and a data structure?
I've started going through the very basic tutorials to learning JAXB, so I'm getting there step by step, but I would really like to be able to use this XML data in another java project I'm working on as soon as possible.
Thanks.
You should start by generating you POJO's. You can use an online tool to do this if you haven't done this already, see How to generate JAXB classes from just XML
I'm not sure what you're trying to accomplish with "private" explanation. First step, get it to unmarshall. You can then change decorations to make appropriate private fields - see How to marshal/unmarshal Java objects with private fields using JAXB
Essentially, use #XmlElement on private members:
public class model {
#XmlAttribute
private String to;

Parsing xml with many attributes using Java JAXB

I have following xml which I am trying to parse using JAXB.
<?xml version="1.0" encoding="UTF-8"?>
<DataSet Name="core_donor" Version="2-0" SchemaVersion="1-0" SchemaDate="2014-01-16">
<DataPoints>
<DataPoint Name="donor_id" Value="105272" />
<DataPoint Name="surname" Value="TWO" />
<DataPoint Name="forename" Value="SCENARIO" />
<DataSubSet Name="blood_gas">
<DataPoints>
<DataPoint Name="blood_gas_no" Value="1" />
<DataPoint Name="blood_gas_date" Value="07/10/2014 10:15" />
</DataPoints>
<DataPoints>
<DataPoint Name="blood_gas_no" Value="2" />
<DataPoint Name="blood_gas_date" Value="07/10/2014 11:20" />
</DataPoints>
</DataSubSet>
<DataSubSet Name="liver_function">
<DataPoints>
<DataPoint Name="liver_function_no" Value="1" />
<DataPoint Name="liver_function_sample_date" Value="07/10/2014 11:10" />
</DataPoints>
<DataPoints>
<DataPoint Name="liver_function_no" Value="2" />
<DataPoint Name="liver_function_sample_date" Value="07/10/2014 13:52" />
</DataPoints>
</DataSubSet>
</DataPoints>
</DataSet>
I would be thankful if someone could point me how to achieve the parsing in java using JAXB.
Thanks!
For starters follow the below example
http://www.mkyong.com/java/jaxb-hello-world-example/
But DataPoints will be your RootElement , if am not wrong also refererence
check some examples for DOM parsing to traverse over the child nodes and read them.

Categories

Resources