Home » VBScript

Using XML in scripting

7 August 2005 10,590 views No Comment

This article is intended to be a fast primer to understanding XML for anyone who hasn’t had the time to investigate its capabilities. XML is a text based file format that allows you to define your own structure within the conventions laid out by the XML format. What follows will hopefully show you the benefits of using XML and how you can use it to manipulate data with minimum effort. How you then apply this to your technical solutions inside and outside of XSI is up to you.

The first part of this article looks at the ideas and concepts behind XML data. The second half will show how easy it is to put it into practice in scripting and C++.

Making sure you have the latest vesion

Before we go on, you should make sure that you have the latest version of Microsoft’s XML support installed. Note that MSXML 4.0 sits on top of the previous versions of MSXML, and so it shouldn’t cause any conflicts with any other software.

To obtain the latest version, I’ve created a web page which should perform the installation quickly and easily through your web browser. Alternatively if you prefer, you can download it directly from Microsoft by visiting this web page here

XML Basics

Without further delay, let’s look at a very simple example of XML before we examine the benefits of using it:

<?xml version="1.0" encoding="utf-8" ?>
<object>
   <name>robot</name>
   <file>c:\project\models\robot.emdl</file>
</object>

The first line is the declaration of the XML file type. At this stage, it’s not worth examining any further, except to say that this is how the file identifies itself as XML.

The second line declares an opening tag with “object” as the identifier. The last line then specifies the corresponding closing tag. All tags must have an open and close tag, unless it is a special XML tag (indicated by a <?, see the first line) or if it contains no child data. If it contains no child data, then as a shortcut it can be written as "<object/>".

The nice indented layout of the example is optional. It wouldn’t matter at all if there were no new lines or white space between the tags. You should note however, that this doesn’t mean that XML ignores the white space. Many XML parsers have by default the option to store the white space as extra child nodes of the parent node, so be warned!

The big advantage of using XML as a file format is scalability. This piece of jargon basically just means that it’s easy to expand on, without needing to have to plan for every eventuality at the outset. For example, if we later decide that we need to store information about the animation of the object, we can just add in an extra set of nodes like this:

<?xml version="1.0" encoding="utf-8" ?>
<object>
   <name>robot</name>
   <file>c:\project\models\robot.emdl</file>
   <animation>
      ...other data...
   </animation>
</object>

Imagine that we had written a script with a function to parse the original <object> node tree structure. If that old function had been written properly, it will now just ignore the new <animation> nodes. If we update the function, we can tell it to read the new <animation> nodes if they exist, or to use some predefined default values if they don’t.

We can also embed the <object> node tree into an entirely different XML tree. For example, we might have an XML file that describes a scene:

<?xml version="1.0" encoding="utf-8" ?>
<scene>
   <numobjects>1</ numobjects >
   <file>c:\project\scenes\robotfight.scn</file>
   <objectlist>
      <object>
         <name>robot</name>
         <file>c:\project\models\robot.emdl</file>
         <animation>
            Öother dataÖ
         </animation>
      </object>
   </objectlist>
</scene>

The point to note here is that we can still use our original function to parse the <object> node sub-tree, even though it’s located in an entirely different file format. This is why XML is so nice to use; it saves so much unnecessary work maintaining input/output (IO) routines.

The next page introduces XSL transforms and shows how you can use them to manipulate your XML data.

1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading...

Leave your response!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

This is a Gravatar-enabled weblog. To get your own globally-recognized-avatar, please register at Gravatar.