Creating a Simple XML Document

MSXML 5.0 SDK

Microsoft XML Core Services (MSXML) 5.0 for Microsoft Office - XML Developer's Guide

Creating a Simple XML Document

To demonstrate how XML structures can be used to break down and describe information, we will create a very basic XML document containing an employee record that includes the following fields.

  • Name
  • Home Address
  • Job Title
  • Salary

This is the sample employee record.

Jim Kim
1234 South Street
Anywhere, NY 10001
USA
Vice President of Finance
$175,000

Defining Basic Elements

To start our conversion to XML, we need a document or root element, in this case, <employeeRecord>, to hold all of our document content. For more information about the document or root element, see Elements.

Next, we will put the employee name into a <name> element; the address into a <homeAddress> element; the title into a <jobTitle element>; and the salary into a <salary> element.

<employeeRecord>
<name>Jim Kim</name>
<homeAddress>1234 South Street
Anywhere, NY 10001
USA</homeAddress>
<jobTitle>Vice President of Finance</jobTitle>
<salary>$175,000</salary>
</employeeRecord>

The beginnings of elements are marked with start tags, like <employeeRecord>, while the ends of elements are marked with end tags, like </employeeRecord>.

All of the elements nest properly. No element contains another element's start tag without including its end tag as well.

Refining Elements

The <name> and <homeAddress> elements contain information that might be useful for sorting and searching, for example, sorting the list by family name or finding all employees who live in a certain country/region or postal code.

We will add some child elements, <givenName>, <middleName>, and <familyName>, to the name element.

<name><givenName>Jim</givenName> <middleName></middleName> <lastName>Kim</lastName></name>

We will also add more detail to our home address element.

<homeAddress><street>1234 South Street</street>
<city>Anywhere</city>, <stateProvince>NY</stateProvince> <postalCode>10001</postalCode>
<country>USA</country></homeAddress>

The document now looks as follows:

<employeeRecord>
<name><givenName>Jim</givenName> <middleName></middleName> <lastName>Kim</lastName></name>
<homeAddress><street>1234 South Street</street>
<city>Anywhere</city>, <stateProvince>NY</stateProvince> <postalCode>10001</postalCode>
<country>USA</country></homeAddress>
<jobTitle>Vice President of Finance</jobTitle>
<salary>$175,000</salary>
</employeeRecord>

In this version, element structures identify all of the document content. If we had content that might not belong in the document or that might not ever need to contain further structure, we might use an attribute. For example, if this company were paying salaries in more than one currency, it might make sense to indicate that the salary presented here is in U.S., not Canadian, dollars. A currency attribute on the salary element lets us indicate that as follows.

<salary currency="USD">$175,000</salary>

In a similar way, an employeeID attribute on the <employeeRecord> element might make it easier to manipulate employee record documents, especially if we had to combine multiple records into a single XML document.

<employeeRecord employeeID="2344-12Z">

The complete document now looks as follows.

<employeeRecord employeeID="2344-12Z">
<name><givenName>Jim</givenName> <middleName></middleName> <lastName>Kim</lastName></name>
<homeAddress><street>1234 South Street</street>
<city>Anywhere</city>, <stateProvince>NY</stateProvince> <postalCode>10001</postalCode>
<country>USA</country></homeAddress>
<jobTitle>Vice President of Finance</jobTitle>
<salary currency="USD">$175,000</salary>
</employeeRecord>