Automating XML Construction
The sample code
for this article stores the parsing rules in an XML file. The application loads the XML file and then extracts the parsing rules from the XML using JAXB. Storing the rules in a database table can be better if you feel using XML and JAXB for getting parsing rules is overkill for your application.
The parsing rules simply inform the parsing application of the name and length of each field in the incoming fixed-width text file.
<?xml version="1.0" encoding="UTF-8"?>
Using the parsing rules from the preceding XML file, the application parses the input text file, and constructs a flat XML
file. A flat XML file is the XML-formatted equivalent of the text file; in other words, it has no hierarchical structure beyond that enforced by XML itselfa root node containing the record structure inherent in the fields of the text file. For example, here's the same incoming patient record:
// Sample flat file Patient appointment data:
// Note: The following code would be one
// line in the incoming flat file.
P JAVA DEVELOPER73774777719740310
20874Programming stress disorders
And here's the flat XML equivalent:
Programming stress disorders
Constructing the flat XML file is always the first step in this generic flat-file parsing application, because the result is well-formed XML that you can then transform into more complex and useful structures using XSLT.
Applying an XSL Transformation
Often, the flat XML file isn't precisely mated to your application's needs. For example, there's little point in searching through the flat XML file to see if a patient exists when you could speed up the operation enormously by extracting only the information required to identify a patient. To create such files, you use XSLT to transform the flat XML file into more appropriate forms. The XSL transformation process consists of three steps.
- Load the XSL document
- Load the source XML document (in this example it's the flat XML file).
- Use an XSL processor to transform the document
There are many different implementations of XML and XSL parsers available for Java; I used the one implemented by Oracle, but you should be able to use any implementation.
Loading an XSL Document
The following Java code parses the XSL document and creates an instance of an XSLStylesheet class.
Loading an XML Document
DOMParser parser = new DOMParser();
// you can also use a standard HTTP URL instead of
// the file protocol shown below
URL xslURL = new URL("file://" + fileName);
XMLDocument xsldoc = parser.getDocument();
// instantiate a stylesheet
XSLStylesheet xsl = new XSLStylesheet(xsldoc, xslURL);
The following code parses the XML string and constructs an XMLDocument object.
Transforming an XSL Document
ByteArrayInputStream theStream =
new ByteArrayInputStream( XMLStr.getBytes() );
XMLDocument xml = parser.getDocument();
After loading a stylesheet and the XML document, you apply the XSL transformation using an XSLProcessor object.
XSLProcessor processor = new XSLProcessor();
DocumentFragment result =
// create an output document to hold the result
XMLDocument out = new XMLDocument();
// create a dummy document element for the
// output document
ByteArrayOutputStream outStream =
new ByteArrayOutputStream( );
String transformedXML = outStream.toString();
Using this generic code, you can construct any number of XML documents from a single source flat XML file just by changing the XSL files. Changing the parsing rules or the XSL file requires no changes to this Java code.