x
 
Create New Page

Revision: XML Parsing Tool

Revision from: 13:06, 15 Jan 2009

I have created a module to parse XML data and turn them into EE variables that you can place in your template . The results can be cached into database for better performance.  It is based on XML_Walk from Solspace(www.solspace.com).  Special thanks to Mitchell.  It is originally written to handle XML data and with a few tweaks, it can also be used to extract data from RSS/Atom feeds.

Overview
- This module will parse a valid remote XML-based file and return entries as EE variables to be used in your EE template.
- This parser will also work as a RSS reader but channel information will be omitted.
- XML data can be cached in database to improve performance.
- Cached data list is viewable under Module Control panel.
- You should use the W3C tool (XML Validator) to validate your XML data feed.
- PHP CURL (Client URL library) is required on web server to run this module.
- This light-weight parser may not work with very large or deep-nested XML files.
- Feel free to modify this module to suit your needs.
- In release 1.1, this parser now supports limit tag, handles duplicated names and conditional variables.

Full documentation can be viewed once you install the module.  Tested on EE 1.6.3.

Downloads
Latest Version
File:ano xml parser 1 1.zip

Previous Versions
File:ano xml parser 1 0 fixed.zip

Example Template Codes
See the following examples to get some ideas if this fits your needs.  Feel free to modify or customize it to suit your needs.  I only implemented it in a way to deal with what I need.  You may need to add some other parameters like “limit” or “backspace” or conditions…etc

Simple XML data feed

{exp:ano_xml_parser:fetch source='http://www.w3schools.com/xml/simple.xml' refresh='60' parent_tag='breakfast_menu' child_tag='food'}
Food Name
{food_name} with only {food_calories} calories<br>
Price{food_price}<br>
Description{food_description}<br>
<
hr>
{/exp:ano_xml_parser:fetch} 

Non standard XML data feed

<h1>National Weather XML Test</h1>

{exp:ano_xml_parser:fetch source='http://www.weather.gov/data/current_obs/KJFK.xml' refresh='0' parent_tag='_no_root_' child_tag='current_observation'}
Credit
{current_observation_credit}<br/>
Location{current_observation_location}<br>
<
img src='{current_observation_image_url}' title='{current_observation_image_title}'><br>
Temperature{current_observation_temperature_string}<br>
<
hr>
{/exp:ano_xml_parser:fetch} 

RSS data feed

<h1>Yahoo RSS News Test</h1>

{exp:ano_xml_parser:fetch source='http://rss.news.yahoo.com/rss/topstories' refresh='0' parent_tag='channel' child_tag='item'}
Title
{item_title}<br/>
Description{item_description}<br>
Link:<a  target='_blank'>{item_link}</a><br>
Is Permanent Link?: {item_guid_isPermaLink}<br>
Media Credit{item_media:credit}<br>
<
hr>
{/exp:ano_xml_parser:fetch} 

Atom data feed

<h1>Expression Engine Developer Atom Test</h1>

{exp:ano_xml_parser:fetch source='http://expressionengine.com/feeds/atom/devblog/' refresh='0' parent_tag='feed' child_tag='entry'}
<u>Title</u>: {entry_title}by author:{entry_author_name}
<u>Category</u>:{category_term}/{category_label}<br>
<
u>Description</u>: {entry_content}<br>
<
hr>
{/exp:ano_xml_parser:fetch} 

FAQ
1. I receive a notice message about “Undefined variable: tag_value in ....”, what’s wrong?
The parser doesn’t handle gracefully for undefined variables.  This happens if you try to access a EE variable that is not formed by the parser via the feed.  Double check your EE tag in the template is based on the naming scheme required by the parser.  For instance, you’ll need to add the child tag suffix in front of your variable.  Eg. child_tag=‘product’, to access the name of the product, you’ll need to use {product_name}, not {name}.

Category:XML

Category:EE1

Categories: