Easily Parse XML

How to Easily Parse XML

XML is used a lot on the internet and in business .  For instance, it is used by Blogs in a format called Real Simple Syndication, or RSS , to allow people to read a blog without actually visiting the blog’s website.  XML is also used to store reference information in web services and configuration files on many open and closed source products such as Instant Messager servers , web servers , and Oracle’s SQL Developer .

Since XML has become so popular in recent years, there are tools for most languages that make XML pretty easy to parse and handle.

Here are 2 tools you can use to parse XML:

1)  MagpieRSS for PHP

MagpieRSS is a great way to parse almost any format of XML clearly and concisely.  Simply include the file "rss_fetch.inc" and then access the entire XML feed as an array.

From their example page:

<item rdf:about="http://protest.net/NorthEast/calendrome.cgi?span=event&ID=210257">
<title>Weekly Peace Vigil</title>
<link>http://protest.net/NorthEast/calendrome.cgi?span=event&ID=210257</link>
<description>Wear a white ribbon</description>
<dc:subject>Peace</dc:subject>
<ev:startdate>2002-06-01T11:00:00</ev:startdate>
<ev:location>Northampton, MA</ev:location>
<ev:enddate>2002-06-01T12:00:00</ev:enddate>
<ev:type>Protest</ev:type>
</item>

Get's parsed to:

array(
title => 'Weekly Peace Vigil',
link => 'http://protest.net/NorthEast/calendrome.cgi?span=event&ID=210257',
description => 'Wear a white ribbon',
dc => array (
subject => 'Peace'
),
ev => array (
startdate => '2002-06-01T11:00:00',
enddate => '2002-06-01T12:00:00',
type => 'Protest',
location => 'Northampton, MA'
)
);

  2) RSSutils Jar and Tag Library for Java

First, download and unpack RSSutils from here
.  Next copy 
rssutils.jar

and rsstaglib.tld
into your 
/WEB-INF/lib
directory.  Then add the following to web.xml
:


    <taglib>
<taglib-uri>/WEB-INF/lib/rssutils.tld</taglib-uri>
<taglib-location>/WEB-INF/lib/rssutils.tld</taglib-location>
</taglib>

Now you can reference the tag library in your JSP page similar to the following example:

    <%@ taglib uri="/WEB-INF/lib/rssutils.tld" prefix="rss" %>

<rss:feed
url="http://url/to/xml.xml" feedId="xmlFeed"/>
<b>Image: </b><rss:channelImage feedId="xmlFeed"/><br>
<b>Title: </b><rss:channelTitle feedId="xmlFeed"/><br>
<b>Link: </b><rss:channelLink feedId="xmlFeed" asLink="true"/><br>
<b>Description: </b><rss:channelDescription feedId="xmlFeed"/><br>
<ul>
<rss:forEachItem feedId="xmlFeed">
<li><rss:itemDescription feedId="xmlFeed"/><br><br>
</rss:forEachItem>
</ul>


Make sure you come up with a unique feedId
to use throughout the page, especially if you are going to be parsing multiple feeds on a single page.  Read a much longer rssutils tutorial here
.

Related Stuff

Eliot is a software developer by trade and a entrepreneur by spirit. He has built web software for the likes of YMCA, UPS, FedEx, Ford and Harcourt, and helped launch mobile apps like Coupon Policy, TourWrist, and Pocket Legal.

Join him as he learns and shares his experiences of marketing his own products online.

Subscribe via RSS
@EliotDill

Comments are closed.