CFMX ships with an XPATH engine that is called using the XMLSearch() function. It's great and absolutely dead simple to use. I've got a few code examples to share that I whipped up playing with http://fullasagoog.com/
XPATH is kind of like the SQL of XML. That's a pretty rude analogy but for developers who are use to extracting stuff from databases and are confronted with the prospect of getting info out of XML documents I think works.
FullAsAGoog.com is a website experiment that aggregates blog feeds together into a single feed. Pulling the excerpts from an RSS XML blog feed for display is very simple using XPATH in CFMX.
For example, lets play with a couple of random RSS 0.91-2 feeds (check the top of the XML document for the version).
- Grab these blog feeds and save them locally
- Use CFFILE to read one of the feeds into a variable like myFile
- Use the XMLParse() function to convert the variable into a CFMX XML Document (eg. <CFSET myXML = XMLParse(myFile)>)
- And if you thought that was easy, use XMLSearch() to extract the item elements as your list of news items like so <CFSET aResults = XMLSearch(myXML, "//item")>
- CFDUMP your aResults and see what you have
The only tricky bit is working out the right location path. In this short example the "//" means look for any element no matter how embedded in the XML document, that corresponds to "item". Blog feeds are a great place to start learning XML - they are very simple in structure and you could put a little news window on your site to boot.
To learn more about XPATH syntax you should have a look at the XML Path Language (XPath) Version 1.0 (http://www.w3.org/TR/xpath). Now normally reading these W3C recommendation should come with a government health warning but his one is really good. Scan through the doc and look for the great list of example "location paths". Then its just a case of hacking the code snippets for the path you need.
Posted by modius at 05:38 PM | Permalink