RequirementsThe general requirement for this utility is to convert a CSV file containing one or more logical documents to one or more XML instance documents, each representing a single logical document. This is a significant improvement over the utility presented in Chapter 3, which was restricted to handling a single logical document. In addition, 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.
It may be fairly obvious why we want to create a different XML document for each logical document in the input CSV file. However, it might be helpful to review why we might want to segregate the XML documents for different trading partners into different directories. Despite the proliferation of "standards" (remember the old line, "I love standards, there are so many to choose from"), it is still customary for different trading partners to require equivalent business data to be processed using different formats. Even when they choose, for example, the same version of a UBL Order, they may use it differently. Given that, we might want to apply different XSLT transformations for the documents we send to different trading partners. Even if the documents are exactly the same format (which is unlikely ), we might still want to segregate instance documents by destination since each trading partner has a unique network destination and perhaps uses different file transfer protocols, security measures, and so on. Separate directories facilitate this segregation . Running the UtilityThis section provides instructions for running the revised CSV to XML 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: Unless otherwise noted, all numeric limits may be modified by changing parameters in the program source and appropriate type definitions in the file description document schemas.
Sample Input and Output: InvoiceFor our sample CSV file in this chapter we use a document commonly exported in electronic commerce situations from small businesses to larger customers that mandate that documents be exchanged electronically . We use a simple invoice. In this example, each row exported from a desktop bookkeeping and order management system has the same format. Each row represents one item to be invoiced, and we want all the rows for a specific invoice number to be grouped together into one invoice document. In addition, we want the invoices destined for different trading partners to be separated from each other, based on the customer ID column. Our sample business is Big Daddy's Gourmet Cocoa, a specialty manufacturer of instant hot chocolate beverages that sells primarily to grocery chains. All the columns in our example invoice (Table 7.1) are required except for the second line of the Ship to Street address (column 8) and the Ship to Country (column 12), which is assumed to be the same as Big Daddy's country (in this case, the United States). Shown below is the sample input invoice that corresponds to the CSV format in Table 7.1. ( Note : Line breaks and indentation have been inserted for readability.) Sample Input CSV File (Invoices.csv)BQ003,2002041,11/12/2002,AZ999345,12/12/2002, "Yazoo Grocers - NE Distribution Center", "12 Industrial Parkway, NW","",Portland,ME,04101,, HCVAN,12,2.59,"Instant Hot Cocoa Mix - Vanilla flavor",31.08 BQ003,2002041,11/12/2002,AZ999345,12/12/2002, "Yazoo Grocers - NE Distribution Center", "12 Industrial Parkway, NW","",Portland,ME,04101,, HCMIN,24,2.53,"Instant Hot Cocoa Mix - Mint flavor",60.72 BQ003,2002042,11/12/2002,AW999346,12/12/2002, "Yazoo Grocers - SE Distribution Center", "Dock 37","3975 Hwy 75",Atoka,OK,74525,, HCVAN,36,2.59,"Instant Hot Cocoa Mix - Vanilla flavor",93.24 BQ003,2002042,11/12/2002,AW999346,12/12/2002, "Yazoo Grocers - SE Distribution Center", "Dock 37","3975 Hwy 75",Atoka,OK,74525,, HCMIN,72,2.53,"Instant Hot Cocoa Mix - Mint flavor",182.16 AY001,2002043,11/12/2002,2002-0967,12/12/2002, "Corner Drug and Sundries", "14 Main Street","",Wichita,KS,67201,, HCVAN,24,2.59,"Instant Hot Cocoa Mix - Vanilla flavor",62.16 BR095,2002044,11/12/2002,4397-0498,12/12/2002, "Big Box Discounters - Store # 97", "37 MegaMall","",Azusa,CA,91702,, HCMIN,120,2.53,"Instant Hot Cocoa Mix - Mint flavor",303.60 BR095,2002044,11/12/2002,4397-0498,12/12/2002, "Big Box Discounters - Store # 97", "37 MegaMall","",Azusa,CA,91702,, HCVAN,360,2.59,"Instant Hot Cocoa Mix - Vanilla flavor",932.40 BR095,2002044,11/12/2002,4397-0498,12/12/2002, "Big Box Discounters - Store # 97", "37 MegaMall","",Azusa,CA,91702,, HCDUC,240,2.59,"Instant Hot Cocoa Mix - Dutch Chocolate flavor",621.60 BR095,2002045,11/12/2002,4345-0498,12/12/2002, "Big Box Discounters - Store # 45", "45 Highway 76","",Branson,MO,65615,, HCMIN,72,2.53,"Instant Hot Cocoa Mix - Mint flavor",182.16 BR095,2002045,11/12/2002,4345-0498,12/12/2002, "Big Box Discounters - Store # 45", "45 Highway 76","",Branson,MO,65615,, HCDUC,96,2.59,"Instant Hot Cocoa Mix - Dutch Chocolate flavor",248.64 DQ349,2002046,11/12/2002,987-43671,12/12/2002, "Maple Leaf Grocers - DC #1", "987 Yorkland Blvd","",Willowdale,ON,M2J 4Y8,CAN, HCMOC,360,2.69,"Instant Hot Cocoa Mix - Mocha flavor",968.40 Table 7.1. Logical Layout for the Invoice
Successful processing should produce XML documents that look like the following. (I show only the first three.) Sample Output Documents<?xml version="1.0" encoding="UTF-8"?> <Invoice xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="CSVInvoice.xsd"> <InvoiceLine> <CustomerNumber>BQ003</CustomerNumber> <InvoiceNumber>2002041</InvoiceNumber> <InvoiceDate>2002-11-12</InvoiceDate> <PONumber>AZ999345</PONumber> <DueDate>2002-12-12</DueDate> <ShipToName>Yazoo Grocers - NE Distribution Center </ShipToName> <ShipToStreet1>12 Industrial Parkway, NW</ShipToStreet1> <ShipToCity>Portland</ShipToCity> <ShipToStateOrProvince>ME</ShipToStateOrProvince> <ShipToPostalCode>04101</ShipToPostalCode> <ItemID>HCVAN</ItemID> <ItemQuantity>12</ItemQuantity> <UnitPrice>2.59</UnitPrice> <ItemDescription>Instant Hot Cocoa Mix - Vanilla flavor </ItemDescription> <ExtendedPrice>31.08</ExtendedPrice> </InvoiceLine> <InvoiceLine> <CustomerNumber>BQ003</CustomerNumber> <InvoiceNumber>2002041</InvoiceNumber> <InvoiceDate>2002-11-12</InvoiceDate> <PONumber>AZ999345</PONumber> <DueDate>2002-12-12</DueDate> <ShipToName>Yazoo Grocers - NE Distribution Center </ShipToName> <ShipToStreet1>12 Industrial Parkway, NW</ShipToStreet1> <ShipToCity>Portland</ShipToCity> <ShipToStateOrProvince>ME</ShipToStateOrProvince> <ShipToPostalCode>04101</ShipToPostalCode> <ItemID>HCMIN</ItemID> <ItemQuantity>24</ItemQuantity> <UnitPrice>2.53</UnitPrice> <ItemDescription>Instant Hot Cocoa Mix - Mint flavor </ItemDescription> <ExtendedPrice>60.72</ExtendedPrice> </InvoiceLine> </Invoice> <?xml version="1.0" encoding="UTF-8"?> <Invoice xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="CSVInvoice.xsd"> <InvoiceLine> <CustomerNumber>BQ003</CustomerNumber> <InvoiceNumber>2002042</InvoiceNumber> <InvoiceDate>2002-11-12</InvoiceDate> <PONumber>AW999346</PONumber> <DueDate>2002-12-12</DueDate> <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> <ItemID>HCVAN</ItemID> <ItemQuantity>36</ItemQuantity> <UnitPrice>2.59</UnitPrice> <ItemDescription>Instant Hot Cocoa Mix - Vanilla flavor </ItemDescription> <ExtendedPrice>93.24</ExtendedPrice> </InvoiceLine> <InvoiceLine> <CustomerNumber>BQ003</CustomerNumber> <InvoiceNumber>2002042</InvoiceNumber> <InvoiceDate>2002-11-12</InvoiceDate> <PONumber>AW999346</PONumber> <DueDate>2002-12-12</DueDate> <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> <ItemID>HCMIN</ItemID> <ItemQuantity>72</ItemQuantity> <UnitPrice>2.53</UnitPrice> <ItemDescription>Instant Hot Cocoa Mix - Mint flavor </ItemDescription> <ExtendedPrice>182.16</ExtendedPrice> </InvoiceLine> </Invoice> <?xml version="1.0" encoding="UTF-8"?> <Invoice xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="CSVInvoice.xsd"> <InvoiceLine> <CustomerNumber>AY001</CustomerNumber> <InvoiceNumber>2002043</InvoiceNumber> <InvoiceDate>2002-11-12</InvoiceDate> <PONumber>2002-0967</PONumber> <DueDate>2002-12-12</DueDate> <ShipToName>Corner Drug and Sundries</ShipToName> <ShipToStreet1>14 Main Street</ShipToStreet1> <ShipToCity>Wichita</ShipToCity> <ShipToStateOrProvince>KS</ShipToStateOrProvince> <ShipToPostalCode>67201</ShipToPostalCode> <ItemID>HCVAN</ItemID> <ItemQuantity>24</ItemQuantity> <UnitPrice>2.59</UnitPrice> <ItemDescription>Instant Hot Cocoa Mix - Vanilla flavor </ItemDescription> <ExtendedPrice>62.16</ExtendedPrice> </InvoiceLine> </Invoice> |