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:
- The X12 EDI input file is 831.x12
- A copy of the XML output, which also appears below, is 831.xml
- The C# source is ConverterOne.cs
- The Visual Basic.Net source is ConverterOne.vb
- The Java source is ConverterOne.java
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:^: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 |




