Log in
Hot & Trending on FMT

XML Parsing with FileMaker and bBox

In order to more easily work with XML data, I’ve added the libxml2 library to version 0.54 of the bBox plug-in. Among other things, libxml2 provides support for XPath 1.0 queries, which can be used to easily extract or set values from XML data, so XPath was my first choice. Although XML has lost a bit of its shine as JSON has come into vogue, XML is still integral to many systems, including FileMaker. Also, the XPath routines can be used for similar syntaxes, so for instance, you might find it helpful for scraping data from a HTML page.

In order to more easily work with XML data, I’ve added the libxml2 library to version 0.54 of the bBox plug-in.

Among other things, libxml2 provides support for XPath 1.0 queries, which can be used to easily extract or set values from XML data, so XPath was my first choice. Although XML has lost a bit of its shine as JSON has come into vogue, XML is still integral to many systems, including FileMaker. Also, the XPath routines can be used for similar syntaxes, so for instance, you might find it helpful for scraping data from a HTML page.

The first two functions aren’t very interesting, but they are essential. XPathInitialize is used to intialize the XPath environment (surprise!) and do the initial parsing of the XML source. XPathFinalize releases the memory used for the tokenized XML and the XPath environment. XPathEvaluate is where most of the fun happens, as this is where we pass in our XPath statements and get back our results.

Getting Started

Even with a few script steps and some simple XPath statements we can do some interesting stuff. To start off, we’ll need some XML to experiment with. I’ll use a modified version of the example XML from wikiboooks.org:

<library>
<description>A list of books useful for people first learning how to build web XML web applications.</description>
<book>
  <title>XQuery</title>
  <author>Priscilla Walmsley</author>
  <description lang="spanish">Este libro es un recorrido muy detallado, a través de y completo del Lenguaje de Consulta del W3C. Cubre todos los aspectos clave de la lengua.</description>
  <format>Trade press</format>
  <license>Commercial</license>
  <list-price>49.95</list-price>
</book>
<book>
  <title>XQuery Examples</title>
  <author>Chris Wallace</author>
  <author>Dan McCreary</author>
  <description lang="english">This book provides a variety of XQuery example programs and is designed to work with the eXist open-source native XML application server.</description>
  <format>Wiki-books</format>
  <license>Creative Commons Sharealike 3.0 Attribution-Non-commercial</license>
  <list-price>29.95</list-price>
</book>
<book>
  <title>XForms Tutorial and Cookbook</title>
  <author>Dan McCreary</author>
  <description lang="english">This book is an excellent guide for anyone that is just beginning to learn the XForms standard.  The book is focused on providing the reader with simple, but complete examples of how to create XForms web applications.</description>
  <format>Wikibook</format>
  <license>Creative Commons Sharealike 3.0 Attribution-Non-commercial</license>
  <list-price>29.95</list-price>
</book>
<book>
  <title>XRX: XForms, Rest and XQuery</title>
  <author>Dan McCreary</author>
  <description lang="english">This book is an overview of the key architectural and design patterns.</description>
  <format>Wikibook</format>
  <license>Creative Commons Sharealike 3.0 Attribution-Non-commercial</license>
  <list-price>29.95</list-price>
</book>
</library>

Setup

We can consider the XML above as our database that we will be running our queries against. The FileMaker script steps required to do a basic query are pretty simple. Here’s a snippet that does most of what you might need:

Set Variable [ $result; bBox_XPathInitializeFromText (0; DEMO::xml_source)
#
If [ $result = "" ]
   Set Variable [ $result; bBox_XPathEvaluate(DEMO::xpath_expression; DEMO::xpath_node_delimiter)
End If
#
Set Variable [ $ignore; Value:bBox_XPathFinalize ] #

XPath Examples

The XPath statements, in their simplest forms, look identical to unix style file paths. These are what you’d be passing as the path_expression above. Our first example just returns all titles from the above XML:

/library/book/title

A slightly more complex form that returns all titles and their retail prices would be (“//” returns the following node from anywhere in the hierarchy):

//title | //list-price

And here we use one of the XPath functions to get the last book in our list:

/library/book[last()]

Conclusion

XML does have its arcane side, so some capabilities will require some effort to learn, and may be beyond what the current implementation in bBox can handle. Also, XPath functions are fairly strict in their interpretation of XML, so any poorly formed XML is likely to be rejected by the bBox_XPathInitialize call. Use the Console utility or go directly to your system log at /var/log/system.log to check for more detailed information. The xmllint command may also be helpful here.

There is quite a bit more that can be done with XPath, so don’t stop looking here for examples. You can also pair up the XPath functions with the bBox’s curl function to pull in remote content.

Simon

Brian Schick

Brian is a highly experienced FileMaker developer with a proven 15-year track record of successfully delivering complex FileMaker-based solutions to a broad range of industries and clients.

Brian also takes great care to listen closely and carefully. Clients have reported time and again that his solutions not only elegantly address their most challenging business needs, but that he has helped them better recognize and understand their key aspects of their own business in the process.

As a developer, Brian is drawn to programming best practices. He is particularly interested in pushing the envelope of FileMaker design to create solutions incorporating the best aspects of traditional desktop applications, web 2.0 memes, and emerging iOS and mobile metaphors.

To find many more in depth articles from Brian and some must have tools and services from Beezwax visit their site at the link below:

Website: www.beezwax.net