KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Find a specific element in an XML document using XPath
PRODUCT: 4D | VERSION: 2004 | PLATFORM: Mac & Win
Published On: October 7, 2004
Your current browser may not allow you to download the asset. We recommend Mozilla Firefox or Google Chrome.
Log In

4D 2003 introduced a set of XML commands that allowed crawling the "DOM" tree of an XML document. To find a specific element in that version, you would have to write your own method that went through the XML reference nodes one by one.

In 2004, a new command named DOM Find XML element allows you to search for a specific node in the document using XPath. For example, suppose you had a document like the following:

<a>
 <b>
  <c>
   <d/>
   <e/>
  </c>
 </b>
</a>

Using the new command, you can easily locate the "b" element:

$a:=DOM Parse XML source("my.xml")
$b:=DOM Find XML element($a;"/a/b")

Likewise, to locate the "e" element from element "a," you would use the following:
$e:=DOM Find XML element($a;"/a/b/c/e")

Or you could locate it from the "b" element:
$e:=DOM Find XML element($b;"/b/c/e")

As you can see, it is much simpler to use this one command as opposed to looping through the document manually looking for a specific element.