ResourcesDOM Level 2 Core Specification, November 13, 2000, by the World Wide Web Consortium. Available online at http://www.w3.org/DOM/DOMTR#dom2, DOM Reference, Microsoft XML Core Services (MSXML) 4.0, by Microsoft Corporation. Online documentation included as part of the MSXML SDK. Available online at http://msdn.microsoft.com/xml. Java API for XML Processing Specification 1.2, by Sun Microsystems. Online Javadoc documentation, included as part of the Java XML Pack Spring 2002. Available online at http://java.sun.com/xml. Xerces2 API Javadoc, by Apache Software Foundation. Online Javadoc documentation, included as part of the Xerces2 SDK. Available online at http://xml.apache.org. |
Chapter 8. Converting Flat Files to and from XML
|
Flat File to XML: Functionality and OperationRequirementsThis utility converts a flat file containing one or more logical documents to one or more XML instance documents, each representing a single logical document. Here's a summary of the required functionality.
Running the UtilityThis section provides instructions for running the flat file to XML conversion utility from the command line. For Java:
or
For C++ on Win32:
or
Options follow the parameters except for the help option, which may be specified by itself. Parameters:
Options:
Restrictions:
Unless
Sample Input and Output: InvoiceAs in Chapter 7, we're going to use invoices from Big Daddy's Gourmet Cocoa for our example. However, Big Daddy has now upgraded to a more capable order management and bookkeeping system. The new system supports a more comprehensive flat, hierarchical file structure than the CSV formats supported by the previous system.
The simple invoice example is
This particular file has variable length records. Although Big Daddy's system uses variable length records, we could just as easily specify a fixed length record. Table 8.1 shows the logical layout of the invoice file. Figure 8.1 shows the sample input invoice flat file. Table 8.1. Logical Layout for the Invoice
Figure 8.1 Sample Input Flat File (Invoices.dat)[View full width] 10 20 30 40 50 60 70 80 Listed below are the first three XML documents produced by the utility from this input file. FlatInvoice001.xml
<?xml version="1.0" encoding="UTF-8"?>
<FlatInvoice
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="FlatInvoice.xsd">
<Header>
<RecordID>HDR</RecordID>
<CustomerNumber>BQ003</CustomerNumber>
<InvoiceNumber>2002041</InvoiceNumber>
<InvoiceDate>2002-11-12</InvoiceDate>
<PONumber>AZ999345</PONumber>
<DueDate>2002-12-12</DueDate>
</Header>
<ShipTo>
<RecordID>SHP</RecordID>
<ShipToName>
Yazoo Grocers - NE Distribution Center
</ShipToName>
<ShipToStreet1>12 Industrial Parkway NW</ShipToStreet1>
<ShipToCity>Portland</ShipToCity>
<ShipToStateOrProvince>ME</ShipToStateOrProvince>
<ShipToPostalCode>04101</ShipToPostalCode>
</ShipTo>
<LineItemGroup>
<LineItem>
<RecordID>LIN</RecordID>
<ItemID>HCVAN</ItemID>
<ItemQuantity>12</ItemQuantity>
<UnitPrice>2.59</UnitPrice>
<ExtendedPrice>31.08</ExtendedPrice>
</LineItem>
<ItemDescription>
<RecordID>DSC</RecordID>
<Description>
Instant Hot Cocoa Mix - Vanilla flavor
</Description>
</ItemDescription>
</LineItemGroup>
<LineItemGroup>
<LineItem>
<RecordID>LIN</RecordID>
<ItemID>HCMIN</ItemID>
<ItemQuantity>24</ItemQuantity>
<UnitPrice>2.53</UnitPrice>
<ExtendedPrice>60.72</ExtendedPrice>
</LineItem>
<ItemDescription>
<RecordID>DSC</RecordID>
<Description>
Instant Hot Cocoa Mix - Mint flavor
</Description>
</ItemDescription>
</LineItemGroup>
<Summary>
<RecordID>SUM</RecordID>
<TotalAmount>91.80</TotalAmount>
<NumberOfLines>2</NumberOfLines>
</Summary>
</FlatInvoice>
FlatInvoice002.xml
<?xml version="1.0" encoding="UTF-8"?>
<FlatInvoice
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="FlatInvoice.xsd">
<Header>
<RecordID>HDR</RecordID>
<CustomerNumber>BQ003</CustomerNumber>
<InvoiceNumber>2002042</InvoiceNumber>
<InvoiceDate>2002-11-12</InvoiceDate>
<PONumber>AW999346</PONumber>
<DueDate>2002-12-12</DueDate>
</Header>
<ShipTo>
<RecordID>SHP</RecordID>
<ShipToName>
Yazoo Grocers - SE Distribution Center
</ShipToName>
<ShipToStreet1>Dock 37</ShipToStreet1>
<ShipToStreet2>3975 Hwy 75</ShipToStreet2>
<ShipToCity>Atoka</ShipToCity>
<ShipToStateOrProvince>OK</ShipToStateOrProvince>
<ShipToPostalCode>74525</ShipToPostalCode>
</ShipTo>
<LineItemGroup>
<LineItem>
<RecordID>LIN</RecordID>
<ItemID>HCVAN</ItemID>
<ItemQuantity>36</ItemQuantity>
<UnitPrice>2.59</UnitPrice>
<ExtendedPrice>93.24</ExtendedPrice>
</LineItem>
<ItemDescription>
<RecordID>DSC</RecordID>
<Description>
Instant Hot Cocoa Mix - Vanilla flavor
</Description>
</ItemDescription>
</LineItemGroup>
<LineItemGroup>
<LineItem>
<RecordID>LIN</RecordID>
<ItemID>HCMIN</ItemID>
<ItemQuantity>72</ItemQuantity>
<UnitPrice>2.53</UnitPrice>
<ExtendedPrice>182.16</ExtendedPrice>
</LineItem>
<ItemDescription>
<RecordID>DSC</RecordID>
<Description>
Instant Hot Cocoa Mix - Mint flavor
</Description>
</ItemDescription>
</LineItemGroup>
<Summary>
<RecordID>SUM</RecordID>
<TotalAmount>275.40</TotalAmount>
<NumberOfLines>2</NumberOfLines>
</Summary>
</FlatInvoice>
FlatInvoice003.xml
<?xml version="1.0" encoding="UTF-8"?>
<FlatInvoice
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="FlatInvoice.xsd">
<Header>
<RecordID>HDR</RecordID>
<CustomerNumber>AY001</CustomerNumber>
<InvoiceNumber>2002043</InvoiceNumber>
<InvoiceDate>2002-11-12</InvoiceDate>
<PONumber>2002-0967</PONumber>
<DueDate>2002-12-12</DueDate>
</Header>
<ShipTo>
<RecordID>SHP</RecordID>
<ShipToName>Corner Drug and Sundries</ShipToName>
<ShipToStreet1>14 Main Street</ShipToStreet1>
<ShipToCity>Wichita</ShipToCity>
<ShipToStateOrProvince>KS</ShipToStateOrProvince>
<ShipToPostalCode>67201</ShipToPostalCode>
</ShipTo>
<LineItemGroup>
<LineItem>
<RecordID>LIN</RecordID>
<ItemID>HCVAN</ItemID>
<ItemQuantity>24</ItemQuantity>
<UnitPrice>2.59</UnitPrice>
<ExtendedPrice>62.16</ExtendedPrice>
</LineItem>
<ItemDescription>
<RecordID>DSC</RecordID>
<Description>
Instant Hot Cocoa Mix - Vanilla flavor
</Description>
</ItemDescription>
</LineItemGroup>
<Summary>
<RecordID>SUM</RecordID>
<TotalAmount>62.16</TotalAmount>
<NumberOfLines>1</NumberOfLines>
</Summary>
</FlatInvoice>
|