Sample Input and Output

We again use the simple address book listing for the example for this utility. Each row is an entry for a person. The columns are listed below.

  1. Last name

  2. First name

  3. Street address, first line

  4. Street address, second line

  5. City

  6. State or province

  7. Zip or postal code

  8. Country

  9. Phone number

  10. E-mail address

Here's a sample input CSV file for an address book in this format (simple.CSV).

"Jones","Mary","312 Renner Road","Apartment C","Richardson","TX","75080",
   "USA","972-996-1051"
"Smith","Sue","Highway 118",,"Terlingua","TX","79852",
  ,,"[email protected]"
"Fred","Finger","PO Box 999",,"Nome","AK","99762",
  ,,"[email protected]"

Successful processing should produce an XML document that looks like the sample shown below.

Sample Output Document (SimpleCSV.xml)
<?xml version="1.0" encoding="UTF-8"?>
<SimpleCSV>
  <Row>
    <Column01>Jones</Column01>
    <Column02>Mary</Column02>
    <Column03>312 Renner Road</Column03>
    <Column04>Apartment C</Column04>
    <Column05>Richardson</Column05>
    <Column06>TX</Column06>
    <Column07>75080</Column07>
    <Column08>USA</Column08>
    <Column09>972-996-1051</Column09>
  </Row>
  <Row>
    <Column01>Smith</Column01>
    <Column02>Sue</Column02>
    <Column03> Highway 118</Column03>
    <Column05>Terlingua</Column05>
    <Column06>TX</Column06>
    <Column07>79852</Column07>
    <Column10>[email protected]</Column10>
  </Row>
  <Row>
    <Column01>Fred</Column01>
    <Column02>Finger</Column02>
    <Column03>PO Box 999</Column03>
    <Column05>Nome</Column05>
    <Column06>AK</Column06>
    <Column07>99762</Column07>
    <Column10>[email protected]</Column10>
  </Row>
</SimpleCSV>

Again, as with our example in the previous chapter, none of the rows have data for all the columns since some of them (such as column 4, the second line of the street address) are optional.

Using techniques that will be discussed in Chapter 10, we can easily convert this output into the following format using XSLT.

Sample Converted Output Document
<?xml version="1.0" encoding="UTF-8"?>
<AddressBook>
  <AddressEntry>
    <LastName>Jones</LastName>
    <FirstName>Mary</FirstName>
    <Street1>312 Renner Road</Street1>
    <Street2>Apartment C</Street2>
    <City>Richardson</City>
    <State>TX</State>
    <ZipCode>75080</ZipCode>
    <County>USA</County>
    <Telephone>972-996-1051</Telephone>
  </AddressEntry>
  <AddressEntry>
    <LastName>Smith</LastName>
    <FirstName>Sue</FirstName>
    <Street1>Highway 118</Street1>
    <City>Terlingua</City>
    <State>TX</State>
    <ZipCode>79852</ZipCode>
    <E-Mail>[email protected]</E-Mail>
  </AddressEntry>
  <AddressEntry>
    <LastName>Fred</LastName>
    <FirstName>Finger</FirstName>
    <Street1>PO Box 999</Street1>
    <City>Nome</City>
    <State>AK</State>
    <ZipCode>99762</ZipCode>
    <E-Mail>[email protected]</E-Mail>
  </AddressEntry>
</AddressBook>

You may ask, “Why not just create this output in the first place?” We certainly could, quite easily. However, this is intended to be a general purpose utility, not one used just to convert address books. So, the generic ColumnXX names are appropriate.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset