
In our first week coding XML you will build a simple address book, with 5 or so records, and use a total of 20 elements. It will follow the rules of XML syntax to be well formed, and with our DTD next week, be valid as well. The file is already built, so I'm hoping that you will modify this one, or embark on your own theme.
<?xml version="1.0" encoding="UTF-8"?>
<address_book></address_book>
<?xml version="1.0" encoding="UTF-8"?> <record>
<name>
<first_name>first name</first_name>
<middle_name>middle name</middle_name>
<last_name>last name</last_name>
<nick_name>nick name</nick_name>
</name>
</record>
In the next section we add the address information.
<?xml version="1.0" encoding="UTF-8"?> <!--address book using nested elements-->
<record>
<name>
<first_name>first name</first_name>
<middle_name>middle name</middle_name>
<last_name>last name</last_name>
<nick_name>nick name</nick_name>
</name>
<address>
<street_address>street address goes here</street_address>
<street_address_detail>apartment number goes here</street_address_detail> <city>city goes here</city>
<state>state goes here</state>
<zipcode>zipcode goes here</zipcode>
</address>
</record>
In the next section we add the contact information.
<?xml version="1.0" encoding="UTF-8"?>
<!--address book using nested elements-->
<record>
<name>
<first_name>first name</first_name>
<middle_name>middle name</middle_name>
<last_name>last name</last_name>
<nick_name>nick name</nick_name>
</name>
<address>
<street_address>street address goes here</street_address>
<street_address_detail>apartment number goes here</street_address_detail> <city>city goes here</city>
<state>state goes here</state>
<zipcode>zipcode goes here</zipcode>
</address>
<contact>
<home_phone>home phone goes here</home_phone>
<work_phone>work phone goes here</work_phone>
<cell_phone>cell phone goes here</cell_phone>
<fax_number>fax number goes here</fax_number>
<email_address>email address goes here</email_address>
</contact>
</record>
In the next section we add the comment information.
<?xml version="1.0" encoding="UTF-8"?>
<!--address book using nested elements-->
<record>
<name>
<first_name>first name</first_name>
<middle_name>middle name</middle_name>
<last_name>last name</last_name>
<nick_name>nick name</nick_name>
</name>
<address>
<street_address>street address goes here</street_address>
<street_address_detail>apartment number goes here</street_address_detail>
<city>city goes here</city>
<state>state goes here</state>
<zipcode>zipcode goes here</zipcode>
</address>
<contact>
<home_phone>home phone goes here</home_phone>
<work_phone>work phone goes here</work_phone>
<cell_phone>cell phone goes here</cell_phone>
<fax_number>fax number goes here</fax_number>
<email_address>email address goes here</email_address>
</contact>
<comments>
<misc_comments>comments go here</misc_comments>
</comments>
</record>
Now that we have all the information defined for one person we need to be able to add more people to build our address book. To do that we will add a record for each person that we add. To distinquish between records we assign an ID to a unique number.
<?xml version="1.0" encoding="UTF-8"?>
<!--address book using nested elements-->
<address_book>
<record ID="1">
<name>
<first_name>first name</first_name>
<middle_name>middle name</middle_name>
<last_name>last name</last_name>
<nick_name>nick name</nick_name>
</name>
<address>
<street_address>street address goes here</street_address>
<street_address_detail>apartment number goes here</street_address_detail> <city>city goes here</city>
<state>state goes here</state>
<zipcode>zipcode goes here</zipcode>
</address>
<contact>
<home_phone>home phone goes here</home_phone>
<work_phone>work phone goes here</work_phone>
<cell_phone>cell phone goes here</cell_phone>
<fax_number>fax number goes here</fax_number>
<email_address>email address goes here</email_address>
</contact>
<comments>
<misc_comments>comments go here</misc_comments>
</comments>
</record>
<record ID="2">
<name>
<first_name>first name</first_name>
<middle_name>middle name</middle_name>
<last_name>last name</last_name>
<nick_name>nick name</nick_name>
</name>
<address>
<street_address>street address goes here</street_address>
<street_address_detail>apartment number goes here</street_address_detail> <city>city goes here</city>
<state>state goes here</state>
<zipcode>zipcode goes here</zipcode>
</address>
<contact>
<home_phone>home phone goes here</home_phone>
<work_phone>work phone goes here</work_phone>
<cell_phone>cell phone goes here</cell_phone>
<fax_number>fax number goes here</fax_number>
<email_address>email address goes here</email_address>
</contact>
<comments>
<misc_comments>comments go here</misc_comments>
</comments>
</record>
</address_book>
<?xml version="1.0" encoding="UTF-8"?> <!-- This is the start of an address book in XML -->
<?xml version="1.0" encoding="UTF-8"?> <!-- ============= an address book in XML =========== -->
<record>
<name>
<first_name>first name</first_name>
<middle_name>middle name</middle_name>
<last_name>last name</last_name>
<nick_name>nick name</nick_name>
</name>
</record>
<!-- ____________ an address book in XML ___________ -->
<?xml version="1.0" encoding="UTF-8"?>
<address_book></address_book>
<?xml version="1.0" encoding="UTF-8"?> <address_book> </address_book> <address_book> </address_book> When rendered an error is generated: XML Parsing Error: junk after document element
Location: http://fgamedia.org/faculty/rdcormia/COIN78/files/xmlExamples/xml6.xml
Line Number 4, Column 1: <address_book>
^
<?xml version="1.0" encoding="UTF-8"?> <address_book>
<record>
<name>
<first_name>first name goes here</first_name>
<middle_name>middle name goes here</middle_name>
<last_name>last name goes here</last_name>
<nick_name>nick name goes here</nick_name>
</name>
</record>
</address_book>
<address_book>
<record>
<names>
<name>#</names>
</name>
</record>
</address_book>
When rendered an error is generated:
XML Parsing Error: mismatched tag. Expected: </name>.
Location: http://fgamedia.org/faculty/rdcormia/COIN78/files/xmlExamples/xml8.xml
Line Number 4, Column 19: <name>#</names>
------------------^
<?xml version="1.0" encoding="UTF-8"?> <name>
<_firstname>first name goes here</_firstname>
<middle_name>middle name goes here</middle_name>
<last_name>last name goes here</last_name>
<nick.name>nick name goes here</nick.name>
<!--
a period "." can be used in the element name - but not at the beginning
-->
</name>
<address_book>
<record ID="1">
<name>
<first_name>first name goes here</first_name>
<middle_name>middle name goes here</middle_name>
<last_name>last name goes here</last_name>
<nick_name>nick name goes here</nick_name>
</name>
</Record>
</address_book>
When rendered an error is generated:
XML Parsing Error: mismatched tag. Expected: </record>.
Location: http://fgamedia.org/faculty/rdcormia/COIN78/files/xmlExamples/xml10.xml
Line Number 12, Column 4:
</Record>
----------^
<?xml version="1.0" encoding="UTF-8"?> <address_book>
<record ID="1">
<!--
the "ID" refers to a record number. The value of that record is "1"
-->
<?xml version="1.0" encoding="UTF-8"?>
<quiz>
<question type="multiple" number="1">
Which is (are) the root element of an HTML document?
<answers>
<answer choice="a"><HTML></answer>
<answer choice="b"></HTML></answer>
<answer choice="c"><HTML> & <body></answer>
<answer choice="d" correct="true"><HTML> & </HTML></answer>
</answers>
</question>
</quiz>
In order for our data to look this way it needs to be written as:
<?xml version="1.0" encoding="UTF-8"?>
<quiz>
<question type="multiple" number="1">
Which is (are) the root element of an HTML document?
<answers>
<answer choice="a"><HTML></answer>
<answer choice="b"></HTML></answer>
<answer choice="c"><HTML> & <body></answer>
<answer choice="d" correct="true"><HTML> & </HTML></answer>
</answers>
</question>
</quiz>
<question type="multiple" number="2">
Which is not a form of living matter?
<answers>
<answer choice="a" ><![CDATA[animal]]></answer>
<answer choice="b"><![CDATA[plant]]></answer>
<answer choice="c" ><![CDATA[bacteria]]></answer>
<answer choice="d" correct="true"><![CDATA[minerals]]></answer>
</answers>
</question>
Nested elements are used to hold child elements, large blocks of text. Humans tend to code with (nested) elements.
In the example below we have elements holding data and other elements referred to as child elements. The data for the <first_name> element is Robert. The element <contact> holds 4 other elements, <first_name>, <last_name>, <nick_name>, and <email_address>.
<contact>
<first_name>Robert</first_name>
<last_name>Cormia</last_name>
<nick_name>Carbon Bob</nick_name>
<email_address>rdcormia@earthlink.net</email_address>
</contact>
Elements may contain attributes which are name/value pairs. You have seen them in the <meta> and the <img> tags. Attributes add 'granularity' to the definition (or description) of data (see mixed elements below) and are usually written by machines. Notice that empty elements close themselves.
<meta name="" value="" />
<meta name="description" value="description of the document" />
<meta name="keywords" value="keywords in the document" />
<meta name ="author" value="author of the document" />
<meta name ="copyright" value="copyright of the document" />
<meta name="fears" value="spiders, snakes, insects" />
<meta name="aptitude" value="interpersonal, instruction, counseling" />
<img src="../../images/notes_xml.jpg" width="100" height="100" alt="XML" />
Elements may contain either attributes and text or attributes and other elements. Notice that mixed elements do NOT close themselves.
<name language="English">Cat</name> <name language="Latin">Cattus</name> <weight units="pounds">150</weight> <weight units="kilograms">68.2</weight>
Go to the week 3 tutorial, then, create two XML models, one nested, and one empty.
Email these files to me as attachments as descirbed in assignment one to rdcormia@earthlink.net.
I encourage you not to build an address book as your 'theme' but if use address_book_nested.xml and address_book_empty.xml files as a start, but please make significant changes to the model.
Copyright © 2009 - 2010 Robert D. Cormia - October 15, 2009