Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.


Change-proof Your Flat-file Processing with XML : Page 3

If you build applications that use text files as input, try this technique for processing them into XML—and simultaneously insulate your applications from changes to the content and structure of the incoming files.




Application Security Testing: An Integral Part of DevOps

Constructing Useful XML Documents with XSL
The examples in the three steps below show how you can use different XSL files to map data from a single flat source XML document into different output documents that serve different needs and accommodate changes.

Step 1: This example constructs a PatientKey.xml file used as a fast method for performing look-ups to see if a specified patient exists.

Flat XML File

<PatientRecord> <RecordType>P</RecordType> <FirstName>JAVA</FirstName> <LastName>DEVELOPER</LastName> <SSN>737747777</SSN> <DOB>19740310</DOB> <DoctorID>20874</DoctorID> <VisitReason> Programming stress disorders </VisitReason> </PatientRecord>

A PatientKey.xsl stylesheet

<xsl:stylesheet version = '1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'> <xsl:template match="PatientRecord"> <patientKey > <LastName> <xsl:value-of select="LastName"/> </LastName> <DOB> <xsl:value-of select="DOB"/> </DOB> </patientKey> </xsl:template> </xsl:stylesheet>

OUTPUT: PatientKey.xml

<patientKey> <LastName>DEVELOPER</LastName> <DOB>19740310</DOB> </patientKey>

Step 2: Construct an XML document containing patient appointments from the same flat XML file, but using a different XSL template.

A PatientAppointment.xsl stylesheet

<xsl:stylesheet version = '1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'> <xsl:template match="PatientRecord"> <patientAppointment> <SSN><xsl:value-of select="SSN"/></SSN> <DoctorID> <xsl:value-of select="DoctorID"/> </DoctorID> <VisitReason> <xsl:value-of select="VisitReason"/> </VisitReason> </patientAppointment> </xsl:template> </xsl:stylesheet>

OUTPUT: PatientAppointment.xml

<patientAppointment> <SSN>737747777</SSN> <DoctorID>20874</DoctorID> <VisitReason> Programming stress disorders </VisitReason> </patientAppointment>

In each step shown above, applying a different stylesheet results in a different output XML document. You could use the PatientKey.xml file to perform fast lookups and the PatientAppointment.xml file to find patients with similar reasons for visits, to find which doctors treated which patients, etc.

Here's the really important point. All this XML and XSLT processing is overkill as long as the format of incoming text file remains the same and the meaning and combination of the patient record fields remains static. You might as well have baked the flat file parse operation into code, populated object arrays with the information, and avoided using XML altogether. The true advantages of all this XML/XSLT processing work becomes apparent when the incoming data or the business needs change.

Accommodating Change
Business needs and information often change suddenly and sometimes radically. Suppose the record format of the incoming flat file changes? If the parse operation were hard-coded, you'd have to change the application to accommodate the changed record structure. But using this technique, you need only adjust the parsing rules XML file. That's a comparatively simple operation.

Business needs and information often change suddenly and sometimes radically.
A more complex type of change occurs when business needs alter the way you use the information. For example, suppose the original company merges with another company, and as a result, the PatientKey class must be altered to use the patient's Social Security Number (SSN) rather than the patient's last name and date of birth. Using the techniques you've seen, making that change is relatively simple; you just change the way you construct the PatientKey.xml file by replacing the XSL file with a new one.

// New PatientKey.xsl file <xsl:stylesheet version = '1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'> <xsl:template match="PatientRecord"> <patientKey > <SSN> <xsl:value-of select="SSN"/> </SSN> </patientKey> </xsl:template> </xsl:stylesheet> // Different PatientKey.xml file result <patientKey> <SSN>737747777</SSN> </patientKey>

Using this design can make you a hero on the development team because the resulting applications can handle changes that would otherwise require major code changes.

Raghu Donepudi, an independent contractor, currently is a technical manager for a federal government agency. He has a master's degree in Computer Science from Lamar University in Texas. He is a Sun-certified Java developer and the author of many software design techniques.
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date