Orb and XML, Part 2

April 23rd, 2009 by quasimodem
Posted in: Programming

Orb now has an XML engine under the hood. So what? Well, I’ll tell ya what!

XPATH.

What the heck is XPATH?
XPATH is a nice way that somebody came up with to denote a specific element (or set of elements) in an XML document.

They work a lot like filesystem paths; if you consider each file on your hard drive to be an element in a hierarchy (which it is) then the path to that file (e.g. C:\Program Files\Orb\OrbEdit.exe) is simply a condensed description of where that file is in the hierarchy. XPATH works the same way.

For example, consider the following XML snippet:

<World ID="3" Locked="False" Visibility="Visible2">
  <Children>
    <Layer ID="1000004" Name="Background">
      <Scale X="64" Y="64"/>
      <Children>
        <WorldSpace ID="2000004">
          <Position X="0" Y="0"/>
        </WorldSpace>
      </Children>
    </Layer>
  </Children>
</World>

If I wanted to talk about the <WorldSpace> element, I could construct an XPATH statement to point to it:

/World/Children/Layer/Children/WorldSpace

Or perhaps I would want to describe the <Position> element with an X value of 0, I could use an XPATH that looks like this:

//Position[@X=0]


What does all this mean for Orb? It means that you can query for sets of Elements within Orb by XPATH.
For example, if I wanted to immediately grab every Sprite in the World, I could easily do so with the following call:

orb.World:getChildren(”//Sprite”);

Or what if I needed to quickly grab every Circle with a positive X value?

orb.World:getChildren(”//Shape[@SubType='Circle']/Position/..[@X > 0]“);


All XPATH usage hinges on the :getChildren() method, which takes and XPATH string an returns a Lua table filled with the matched Elements. This gives your scripts a lot of power to quickly grab very specific sets of Elements. It’s pretty easy to use once you get a good grasp of XPATH and makes good use of the underlying XML structure. So far so good!


Leave a Reply