Home > Learn XML > XML Tutorials > Data Integration Tutorials > Writing Custom XML Adapters
Data Integration Tutorials
Writing Custom XML AdaptersBy: Tony Lavinio, Software Architect, Principal, The Stylus Studio® Team Overview: Accessing CSV Data as XML Using a Custom On-the-fly ConverterThere are cases where the input format is too complicated for Convert to XML, or both reading and writing of the file format is needed. For those cases, Stylus Studio® XML Enterprise Edition allows you to write and hook in your own adapters. This section will take you through the process of writing a medium-sized adapter in Java. By the end of this, you should have a basic knowledge of how to write a bidirectional adapter, and get a copy of the source code for a CVS-to-XML-and-back adapter. Creating the CSV to XML Converter Class StubsAll adapters in Stylus Studio® are derived from a base class
import java.io.IOException; If this were a read-only adapter, we could omit the fromXML() method, and when a user tried to write through the adapter, the framework would automatically catch the problem and inform the user. Similarly, we support write-only adapters, which are implemented by omitting the toXML() method.
Looking at the Input and Output FormatsOur CSV adapter will support the following features:
Our CSV file is going to look like this: bmw,2004,14274 And our generated XML is going to use the following structure:
<?xml version="1.0" encoding="UTF-8"?> Reading the CSV File (the toXML() method)The code for doing this can be seen here, but the pseudocode for it is this:
If you look carefully at the
real code,
you will also notice another convenience function, Writing the CSV File (the fromXML() method)In this case, we need only a small stub to call our SAX content handler class. Most of the activity will happen there.
The only important thing to remember is not to ignore any exceptions. They can be passed
back up to the user interface using the
public void fromXML(InputStream in, OutputStream out) Our
CommaHandler
class is derived from After keeping a local reference of the output stream passed to it, here is how CommaHandler deals with the various SAX events that come in: CSV to XML SAX startElementWhen
startElement
is called, we keep track of how deeply nested we are. Any time we start a new second-level
node, we create a new row. Anytime we see a third-level row start, we start a field. This means we don't
really care whether our XML looks like
CSV to XML SAX endElementWhen endElement is called for the end of a second-level element, we put out a line feed to finish the line we've been writing. When it is called for the end of a third-level element, we check the text we've accumulated under the characters method. If it doesn't contain a comma or quote character, we can just emit it. Otherwise, we've got to quote it and escape any embedded quotes. We also have to remember that if we're not the first field in the row to put out a comma first to separate ourselves from the previous field. CSV to XML SAX charactersIn the
characters
method, we append the characters we see to the current CSV to XML SAX endDocumentWe can speed up things slightly by flushing the stream when we are done with it when we see the endDocument event. The Completed CSV to XML Conversion Adapter Class Java Source CodeAt this point, we've completed the code for our adapter. The full source code can be seen here: CSV.java. We just need to compile it and tell Stylus Studio® XML Profession Edition that it is available. To compile it, we'll assume that Stylus Studio® is installed in the usual place
The JARs we need on the classpath are all in the bin directory under that, namely:
So, this little
@echo off Assuming we put those files into (click image to enlarge) Stylus Studio® will search the path and list all of the adapters found, so that your screen will look like this, with our newly-added adapter Comma showing clearly. ("CSV" is a similar class that we ship as part of Stylus Studio®.) (click image to enlarge) Using the Newly-Created CSV to XML Adapter with Stylus Studio®To open a sample document with our new adapter, we use the File > Open dialog and build a URL just like for any other adapter:
(click image to enlarge) Now using this URL, you can read and write CSV files as if they were XML. And more importantly, you may use them as input to XSLT or XQuery, or output from those transformations, as easily as any XML document. Using the Adapter as a Java CSV to XML Conversion ToolSince Stylus Studio® exposes the Java API to the StylusFile class, this newly-created class is now callable from Java code through the same interface as any other Stylus Studio® adapter: URL. See Invoking an Adapter Programmatically in the documentation for more details, but a simple example showing the CSV adapter we just wrote is right here:
// Java Source Code for a Stylus Studio® CSV to XML Converter
|
PURCHASE STYLUS STUDIO ONLINE TODAY!!Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced! What's New for Stylus Studio® X16?New XQuery & Web Services Tools, Support for MySQL, PostgreSQL, HL7 EDI, Microsoft .NET Code Generation and much more! Ask Someone You KnowDoes your company use Stylus Studio? Do your competitors? Engineers from over 100,000 leading companies use Stylus Studio, and now you can ask someone from your own organization about their experiences using Stylus Studio. |