Well Formed XML Documents

When you create or modify an XML document, you must make sure that your XML file is well-formed. See Tip 17-5 for what makes a document well-formed. The well-formedness of an XML document is similar to syntax checking in VBA. When you try to open an XLM file in Excel that is not well-formed, you will receive an error message similar to the one in Figure 17-5. I have forced this error by removing the end tag </CELL> from the Units.xml file while it was opened in Notepad. I then resaved the file in Notepad and closed it. When I attempted to open the file again in Excel, I received the error message in Figure 17-5. As you can see, the error dialog box specifies the type of error that was found and indicates that the error log XMLErr.log file can be found in the Temp folder. Figure 17-6 displays the contents of the error log file. You must fix all the errors to successfully open the file in Excel.

Tip 17-5: What is a Well-Formed XML Document?

An XML document must have one root element. While in HTML the root element is always <HTML>, in the XML document you can name your root element anything you want. Element names must begin with a letter or underscore character.

The root element must enclose all other elements. Elements must be properly nested. The XML data must be hierarchical; the beginning and ending tags cannot overlap.

<Employee>

<Employee Id>090909</Employee Id> </Employee>

All element tags must be closed. A begin tag must be followed by an end tag:

<Sessions>5</Sessions>

You can use shortcuts, such as a single slash (/), to end the tag so you don't have to type the full tag name. For example, if the current Sessions element is empty (does not have value), you could use the following tag: <Sessions />.

Tag names are case-sensitive: The tags <Title> and </Title> aren't equivalent to <TITLE> and </TITLE>. For example, the following:

<Title>Beginning VBA Programming </Title>

is not the same as:

<TITLE>Beginning VBA Programming </TITLE>

All attributes must be in quotation marks:

<Course Id="VBAEX1"/>

You cannot have more than one attribute with the same name within the same element. If the <Course> element has two Id attributes, they must be written separately, as shown below:

<Course Id="VBAEX1"/> <Course Id="VBAEX2"/>

0 0

Post a comment