RequirementsThe general requirement for this utility is to convert one or more XML instance documents, each representing a single logical document, into a single CSV file. As with the CSV to XML utility, this is a significant improvement over the utility presented in Chapter 2. Again, many of the restrictions on both the CSV file format and the grammar of the resulting XML document are removed. Here's a summary of the required functionality.
Running the UtilityThis section provides instructions for running the revised XML to CSV Converter 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: The restrictions are the same as those specified for the CSV to XML utility, plus the following.
Sample Input and Output: Purchase OrderFollowing through with our simple procurement example, we'll use a purchase order as the sample document. (See Table 7.2 for the layout. Note that the Description column in this table includes a required or optional designation. This is something we'll be concerned about validating when importing into the application but not when we export from it.) We'll have several XML documents representing purchase orders from different customers, each ordering the gourmet hot chocolate mixes . We want to convert the XML documents to CSV format, in a single file, so that we can import them into our desktop bookkeeping and order management system. Here are three purchase orders that follow this logical organization. Sample PurchaseOrders01.xml<?xml version="1.0" encoding="UTF-8"?> <PurchaseOrder> <POLine> <CustomerNumber>BQ003</CustomerNumber> <PONumber>AZ999345</PONumber> <PODate>2002-11-12</PODate> <RequestedDeliveryDate>2002-11-15</RequestedDeliveryDate> <ShipToName> Yazoo Grocers - NE Distribution Center </ShipToName> <ShipToStreet1>12 Industrial Parkway, NW</ShipToStreet1> <ShipToCity>Portland</ShipToCity> <ShipToState>ME</ShipToState> <ShipToPostalCode>04101</ShipToPostalCode> <ItemID>HCVAN</ItemID> <OrderedQty>12</OrderedQty> <UnitPrice>2.59</UnitPrice> <ItemDescription> Instant Hot Cocoa Mix - Vanilla flavor </ItemDescription> </POLine> <POLine> <CustomerNumber>BQ003</CustomerNumber> <PONumber>AZ999345</PONumber> <PODate>2002-11-12</PODate> <RequestedDeliveryDate>2002-11-15</RequestedDeliveryDate> <ShipToName> Yazoo Grocers - NE Distribution Center </ShipToName> <ShipToStreet1>12 Industrial Parkway, NW</ShipToStreet1> <ShipToCity>Portland</ShipToCity> <ShipToState>ME</ShipToState> <ShipToPostalCode>04101</ShipToPostalCode> <ItemID>HCMIN</ItemID> <OrderedQty>24</OrderedQty> <UnitPrice>2.53</UnitPrice> <ItemDescription> Instant Hot Cocoa Mix - Mint flavor </ItemDescription> </POLine> </PurchaseOrder> Table 7.2. Logical Layout for the Purchase Order
Sample PurchaseOrders02.xml<?xml version="1.0" encoding="UTF-8"?> <PurchaseOrder> <POLine> <CustomerNumber>BQ003</CustomerNumber> <PONumber>AW999346</PONumber> <PODate>2002-11-12</PODate> <RequestedDeliveryDate>2002-11-15</RequestedDeliveryDate> <ShipToName> Yazoo Grocers - SE Distribution Center </ShipToName> <ShipToStreet1>Dock 37</ShipToStreet1> <ShipToStreet2>3975 Hwy 75</ShipToStreet2> <ShipToCity>Atoka</ShipToCity> <ShipToState>OK</ShipToState> <ShipToPostalCode>74525</ShipToPostalCode> <ItemID>HCVAN</ItemID> <OrderedQty>36</OrderedQty> <UnitPrice>2.59</UnitPrice> <ItemDescription> Instant Hot Cocoa Mix - Vanilla flavor </ItemDescription> </POLine> <POLine> <CustomerNumber>BQ003</CustomerNumber> <PONumber>AW999346</PONumber> <PODate>2002-11-12</PODate> <RequestedDeliveryDate>2002-11-15</RequestedDeliveryDate> <ShipToName> Yazoo Grocers - SE Distribution Center </ShipToName> <ShipToStreet1>Dock 37</ShipToStreet1> <ShipToStreet2>3975 Hwy 75</ShipToStreet2> <ShipToCity>Atoka</ShipToCity> <ShipToState>OK</ShipToState> <ShipToPostalCode>74525</ShipToPostalCode> <ItemID>HCMIN</ItemID> <OrderedQty>72</OrderedQty> <UnitPrice>2.53</UnitPrice> <ItemDescription> Instant Hot Cocoa Mix - Mint flavor </ItemDescription> </POLine> </PurchaseOrder> Sample PurchaseOrders03.xml<?xml version="1.0" encoding="UTF-8"?> <PurchaseOrder> <POLine> <CustomerNumber>AY001</CustomerNumber> <PONumber>2002-0967</PONumber> <PODate>2002-11-12</PODate> <RequestedDeliveryDate>2002-11-14</RequestedDeliveryDate> <ShipToName>Corner Drug and Sundries</ShipToName> <ShipToStreet1>14 Main Street</ShipToStreet1> <ShipToCity>Wichita</ShipToCity> <ShipToState>KS</ShipToState> <ShipToPostalCode>67201</ShipToPostalCode> <ItemID>HCVAN</ItemID> <OrderedQty>24</OrderedQty> <UnitPrice>2.59</UnitPrice> <ItemDescription> Instant Hot Cocoa Mix - Vanilla flavor </ItemDescription> </POLine> </PurchaseOrder> Successful processing of these three documents should produce a CSV file that looks like the one shown below. ( Note : Line breaks and indentation have been inserted for readability.) Sample Output CSV FileBQ003,AZ999345,11/12/2002,11/15/2002, "Yazoo Grocers - NE Distribution Center", "12 Industrial Parkway, NW",,"Portland",ME,04101,, HCVAN,12,2.59,"Instant Hot Cocoa Mix - Vanilla flavor" BQ003,AZ999345,11/12/2002,11/15/2002, "Yazoo Grocers - NE Distribution Center", "12 Industrial Parkway, NW",,"Portland",ME,04101,, HCMIN,24,2.53,"Instant Hot Cocoa Mix - Mint flavor" BQ003,AW999346,11/12/2002,11/15/2002, "Yazoo Grocers - SE Distribution Center", "Dock 37","3975 Hwy 75","Atoka",OK,74525,, HCVAN,36,2.59,"Instant Hot Cocoa Mix - Vanilla flavor" BQ003,AW999346,11/12/2002,11/15/2002, "Yazoo Grocers - SE Distribution Center", "Dock 37","3975 Hwy 75","Atoka",OK,74525,, HCMIN,72,2.53,"Instant Hot Cocoa Mix - Mint flavor" AY001,2002-0967,11/12/2002,11/14/2002, "Corner Drug and Sundries", "14 Main Street",,"Wichita",KS,67201,, HCVAN,24,2.59,"Instant Hot Cocoa Mix - Vanilla flavor" |