|
Home:Developers:Tutorials & Examples:Sample XML Conversions
Sample XML Conversions using the XML Converter API
XML Conversions with Minimal Coding
How much code does it take to transform EDI into XML?
Often data just isn't in the right format.
It's in EDI when you need XML.
It's in XML when you need JSON.
It's in CSV when you need it to be in anything but.
The DataDirect XML Converters™ API is a small set of class that let
you easily take information in one format and apply the full power of the conversion library
without having to get tangled in the details.
Suppose you want to convert X12 EDI into XML. To keep things simple, let's write a little
command-line tool that takes X12 from one file and sends the equivalent XML to another. How
much — or how little — code should that take? How about 10 lines of Java:
1 import javax.xml.transform.stream.*;
2 import com.ddtek.xmlconverter.*;
3 public class ConverterOne {
4 public static void main(String[] args) throws Throwable {
5 ConverterFactory factory = new ConverterFactory();
6 ConvertToXML toXML = factory.newConvertToXML("converter:EDI");
7 toXML.convert(new StreamSource(args[0]), new StreamResult(args[1]));
8 System.out.println(args[0] + " --> " + args[1]);
9 }
10 } |
You say Java's not your thing? How about 10 lines of C#:
1 using System;
2 using DDTek.XmlConverter;
3 public class ConverterOne {
4 public static void Main(String[] args) {
5 ConverterFactory factory = new ConverterFactory();
6 ConvertToXml toXML = factory.CreateConvertToXml("converter:EDI");
7 toXML.Convert(new UriSource(args[0]), new UriResult(args[1]));
8 Console.WriteLine(args[0] + " --> " + args[1]);
9 }
10 } |
or even 10 lines of Visual Basic (VB.Net):
1 Imports DDTek.XmlConverter
2
3 Module ConverterOne
4 Sub Main(ByVal args() As String)
5 Dim factory As ConverterFactory = New ConverterFactory()
6 Dim toXml As ConvertToXml = factory.CreateConvertToXml("converter:EDI")
7 toXml.Convert(New UriSource(args(0)), New UriResult(args(1)))
8 System.Console.WriteLine(args(0) + " --> " + args(1))
9 End Sub
10 End Module |
How Does It Work?
What does this do? Let's do a line-by-line.
- Line 5 in all three examples creates a factory object, which
is an object whose sole purpose in life is to create other objects. (That's a design pattern that lets us change the
way we create objects later on without changing the code that uses those objects.)
- Line 6 creates the object which will actually do the conversion. There is also a corresponding from-XML object and method, to go from XML to the native format.
- Line 7 tells the converter to convert, using the two filenames supplied on the command line as input and output respectively.
- Line 8 just tells us that things went as planned.
How would we change this program to handle
EDIFACT instead of
X12? We wouldn't change a thing. The EDI XML Converter
is intelligent, in that it automatically detects the variant of EDI as well as the version, and automatically
selects the correct dictionaries. There is built-in support for IATA
and EANCOM as well.
So what we've written above is a general-purpose, command-line program that will convert almost any standard
EDI document to XML — in ten lines of code!
XML Converters All Growed Up
There are other types of converters as well.
- The EDI converter allows you to extend support beyond the basic vocabularies. If you need to support EDIG@S or MEDEUR
or ATIS or some other variant, comprehensive extensibility mechanisms are available for you.
- dBase converters for a variety of versions of dBase-like formats are included.
- DIF and SYLK spreadsheet formats are also part of the suite.
- OpenEdge export format, from our sister OpenEdge company, is of course inside as well.
- Formats for reading and writing binary and Base-64 data are useful, as are
- Support for JSON, Java .properties files and Windows .ini files.
- Comma-separated and Tab-separated files are supported.
- And for anything we've missed above, you can create Custom XML Convertions
XML Conversion Source Files
If you want to play with this example, here are the source files used on this page:
XML Conversion Input and Output
Here is just a dump of the EDI for 831.x12:
ISA:00: :00: :01:1515151515 :01:5151515151 :041201:1217:U:00403:000032123:0:P:*~
GS:CT:9988776655:1122334455:20041201:1217:128:X:004030~
ST:831:00128001~
BGN:00:88200001:20041201~
N9:BT:88200001~
TRN:1:88200001~
AMT:2:100000.00~
QTY:46:1~
SE:7:00128001~
GE:1:128~
IEA:1:000032123~
|
The XML Converters are very polite; by default they when emit XML for EDI
they include comments so that it is easy to see if the information you think
you are getting is really what you are getting. Below is what the output
from the 831.x12 to 831.xml conversion looks like:
<?xml version="1.0" encoding="UTF-8"?>
<X12>
<ISA>
<ISA01><!--I01: Authorization Information Qualifier-->00<!--No Authorization Information Present (No Meaningful Information in I02)--></ISA01>
<ISA02><!--I02: Authorization Information--></ISA02>
<ISA03><!--I03: Security Information Qualifier-->00<!--No Security Information Present (No Meaningful Information in I04)--></ISA03>
<ISA04><!--I04: Security Information--></ISA04>
<ISA05><!--I05: Interchange ID Qualifier-->01<!--Duns (Dun & Bradstreet)--></ISA05>
<ISA06><!--I06: Interchange Sender ID-->1515151515</ISA06>
<ISA07><!--I05: Interchange ID Qualifier-->01<!--Duns (Dun & Bradstreet)--></ISA07>
<ISA08><!--I07: Interchange Receiver ID-->5151515151</ISA08>
<ISA09><!--I08: Interchange Date-->041201<!--2004-12-01--></ISA09>
<ISA10><!--I09: Interchange Time-->1217<!--12:17:00.00--></ISA10>
<ISA11><!--I65: Repetition Separator-->^</ISA11>
<ISA12><!--I11: Interchange Control Version-->00403<!--Draft Standards for Trial Use Approved for Publication by ASC X12 Procedures Review Board through October 1999--></ISA12>
<ISA13><!--I12: Interchange Control Number-->000032123</ISA13>
<ISA14><!--I13: Acknowledgment Requested-->0<!--No Acknowledgment Requested--></ISA14>
<ISA15><!--I14: Usage Indicator-->P<!--Production Data--></ISA15>
<ISA16><!--I15: Component Element Separator-->*</ISA16>
</ISA>
<GS>
<GS01><!--479: Functional Identifier Code-->CT<!--Application Control Totals (831)--></GS01>
<GS02><!--142: Application Sender's Code-->9988776655</GS02>
<GS03><!--124: Application Receiver's Code-->1122334455</GS03>
<GS04><!--373: Date-->20041201<!--2004-12-01--></GS04>
<GS05><!--337: Time-->1217<!--12:17:00.00--></GS05>
<GS06><!--28: Group Control Number-->128</GS06>
<GS07><!--455: Responsible Agency Code-->X<!--Accredited Standards Committee X12--></GS07>
<GS08><!--480: Version / Release / Industry-->004030<!--Draft Standards Approved for Publication by ASC X12 Procedures Review Board through October 1999--></GS08>
</GS>
<TS_831>
<ST>
<ST01><!--143: Transaction Set Identifier Code-->831<!--Application Control Totals--></ST01>
<ST02><!--329: Transaction Set Control Number-->00128001</ST02>
</ST>
<BGN>
<BGN01><!--353: Transaction Set Purpose Code-->00<!--Original--></BGN01>
<BGN02><!--127: Reference Identification-->88200001</BGN02>
<BGN03><!--373: Date-->20041201<!--2004-12-01--></BGN03>
</BGN>
<N9>
<N901><!--128: Reference Identification Qualifier-->BT<!--Batch Number--></N901>
<N902><!--127: Reference Identification-->88200001</N902>
</N9>
<TRN>
<TRN01><!--481: Trace Type Code-->1<!--Current Transaction Trace Numbers--></TRN01>
<TRN02><!--127: Reference Identification-->88200001</TRN02>
</TRN>
<GROUP_1>
<AMT>
<AMT01><!--522: Amount Qualifier Code-->2<!--Batch Total--></AMT01>
<AMT02><!--782: Monetary Amount-->100000</AMT02>
</AMT>
<QTY>
<QTY01><!--673: Quantity Qualifier-->46<!--Total transactions--></QTY01>
<QTY02><!--380: Quantity-->1</QTY02>
</QTY>
</GROUP_1>
<SE>
<SE01><!--96: Number of Included Segments-->7</SE01>
<SE02><!--329: Transaction Set Control Number-->00128001</SE02>
</SE>
</TS_831>
<GE>
<GE01><!--97: Number of Transaction Sets-->1</GE01>
<GE02><!--28: Group Control Number-->128</GE02>
</GE>
<IEA>
<IEA01><!--I16: Number of Included Functional-->1</IEA01>
<IEA02><!--I12: Interchange Control Number-->000032123</IEA02>
</IEA>
</X12>
|
|