Creating XML Data Files

 < Day Day Up > 



A schema is only one half of what you need to work with XML data; the other half of the equation is the data itself. After you have created a schema that reflects the structure of your data, you can write your data into a file with tags corresponding to the outline in your schema file. The following listing, which you can type into a Notepad file, contains the data for the first two suppliers in the MySuppliers.xml file included on the CD-ROM:

<?xml version="1.0" encoding="UTF-8" ?> 
<Root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" <;$RD> xsi:noNamespaceSchemaLocation="MySuppliers.xsd">

<Supplier>
<SupplierID>1</SupplierID>
<CompanyName>Exotic Liquids</CompanyName>
<ContactName>Charlotte Cooper</ContactName>
<ContactTitle>Purchasing Manager</ContactTitle>
<MailingAddress>
<Address>49 Gilbert St.</Address>
<City>London</City>
<Region></Region>
<PostalCode>EC1 4SD</PostalCode>
<Country>UK</Country>
</MailingAddress>
<Phone>(171) 555-2222</Phone>
<Fax></Fax>
</Supplier>

<Supplier>
<SupplierID>2</SupplierID>
<CompanyName>New Orleans Cajun Delights</CompanyName>
<ContactName>Shelley Burke</ContactName>
<ContactTitle>Order Administrator</ContactTitle>
<MailingAddress>
<Address>P.O. Box 78934</Address>
<City>New Orleans</City>
<Region>LA</Region>
<PostalCode>70117</PostalCode>
<Country>USA</Country>
</MailingAddress>
<Phone>(100) 555-4822</Phone>
<Fax></Fax>
</Supplier>
</Root>

To save your data as an XML file, follow these steps in Notepad:

  1. Click File, Save.

  2. Click the Save As Type down arrow and click All Files.

  3. Type the name of the file in the File Name box. For example, if the name of the file you want to save as an XML file is MySuppliers.xml, you would type MySuppliers.xml.

There are several aspects of the data in the MySuppliers.xml file that you should notice. For example, the first two lines of the data file provide information similar to the first two lines in the schema file.

<?xml version="1.0" encoding="UTF-8" ?>  <Root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  <;$RD> xsi:noNamespaceSchemaLocation="MySuppliers.xsd">

The first line indicates the XML version and the text-encoding scheme used in the file, and the second line indicates the base element in the schema (Root), the URL indicating the standards document to which the data file conforms, and the name of the schema file (MySuppliers.xsd).

Note 

You can use the first two lines of the sample MySuppliers.xml file in your own XML files. All you need to do is open the MySuppliers.xml file in a text editor, copy the lines, paste them into your other XML file, and replace MySuppliers.xsd with the name of your schema file.

The next section of the file contains the data used to populate your worksheet; it's no surprise that the structure of the data in MySuppliers.xml conforms exactly to the structure of the schema presented in MySuppliers.xsd (which you can find on the companion CD-ROM). For example, the schema file has the CompanyName element defined in the following line:

<element name="CompanyName' type="string"/>

And the data file has a corresponding line in each set of supplier data:

<CompanyName>New Orleans Cajun Delights</CompanyName>

Not every element has to contain data, but every element does need to occur within each repetition of the Supplier element. For example, the Fax element in both of the sample data sets contains no value, but the tags do have to occur in the XML data file so that the sequence of elements occurs exactly as defined in the schema file.



 < Day Day Up > 



Microsoft Excel 2003 Programming Inside Out
Microsoft Office Excel 2003 Programming Inside Out (Inside Out (Microsoft))
ISBN: 0735619859
EAN: 2147483647
Year: 2006
Pages: 161

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net