Developers |
Home:Developers:Tutorials & Examples:Adapters Unleashed DataDirect XML Converters™ Adapters UnleashedBy: Ivan Pedruzzi, Product Architect, The DataDirect XML Converters™ Team. If you have processed XML using Java, you should be familiar with JAXP (Java API for XML Processing). Here is a simple program that processes an XML document using XSLT:
import javax.xml.transform.*;Nice and easy if you are dealing with XML documents, but what happens if your data is not all XML? Assume for instance that we need to exchange data with a business partner who requires files using a specific XML dialect, but that our data is tabular and is stored as CSV files. Accessing Non-XML Files as XML in JavaHere's a sample from our CSV file of used motorcycles inventory:
make,model,year,mileageOur partner requires an XML document that looks like what follows, and has provided an XML Schema so that we can ensure that the XML we provide complies with his requirements:
<?xml version="1.0"?>The first step is simply to create an XML representation of our CSV data.
<?xml version="1.0"?>Though some of the specifics vary (additional parent elements, different element names, and so on), because the data is now in XML format, XSLT can be used to create the final XML result. So, that's the theory (CSV to> XML to> XSLT to> XML), but how do you make it all work? Even assuming you have an XML document that represents data stored in CSV format, the prospect of writing XSLT can be daunting. It might sound intimidating, but using DataDirect XML Converters™ we can create solid, complex, bug-free XSLT in just a few steps.
You know what they say ... a picture is worth 1000 words. DataDirect XML Converters™ did most of the work for us — converting CSV to XML, writing XSLT code based on simple mappings — but what happens if we need to perform the same steps periodically, perhaps as part of larger application so that we can automate the process of exchanging inventory information with our partner? The answer lies in common URLs, and DataDirect XML Converters™'s uncommon Java runtime library. Automating Non-XML Data Access in Java ApplicationsURL stands for Uniform Resource Locator. URLs allow us to use a symbolic name to reach a document that either exists somewhere or gets created dynamically when the address represented in the URL is hit. If you look closely inside the DataDirect XML Converters™ Project window, you quickly realize how adapters work — an adapter is represented as a URL, like FTP, HTTP, FILE, and so on. Let's take a closer look at an adapter's components, using the following adapter as an example:
Using The DataDirect XML Converters™ Runtime LibraryLet's go back to the original JAXP example. Wouldn't it be cool if we could use the adapter URL to specify the argument for the Java StreamSource method — that is, to effectively use a CSV file as a source for an XSLT transformation, and do it all programmatically? Thanks to the DataDirect XML Converters™ runtime library, we can!
import javax.xml.transform.*;JAXP defines a clever mechanism that allows XSLT processors to delegate URL resolution to an
external component called the The
<xsl:for-each select="document('
adapter:CSV:newline=crlf:sep=,:first=yes:To register a
TransformerFactory tf = TransformerFactory.newInstance();We hope you enjoyed this overview of DataDirect XML Converters™'s runtime library. Happy Java programming! Ivan Pedruzzi, |
DataDirect XML Converters™ NewsVisit our Media Coverage page to scroll through a list of news articles and to read what the media has been saying about DataDirect XML Converters. Try DataDirect XML Converters™ Free!Put the power, scalability, and performance of DataDirect XML Converters™ to work for you today! Our free trial lets you see for yourself how easy it is to develop scalable data integration applications that convert relational, EDI, and other file formats into XML! |








