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
.

Call Web Service in Java

Consume a Web Service with Java

As I stated before, calling a web service in .Net is easy .  However, using a Java Web Service is a little more difficult .  This is not because .Net is superior to Java, but instead because of the various standards that Java allows for since it is open sourced.  In other words, Java can be extended and new standards can be developed, whereas Microsoft keeps tight control over their beloved .Net framework.

Step 1:  Download and Install NetBeans

First you must download and Java IDE such as Netbeans , Eclipse , or Oracle’s JDeveloper .  I have used all three and I would recommend NetBeans because I found that JDeveloper was a little behind on certian standards and Eclipse requires that you install additional plugins to allow you to develop with web services.  However, the latest version of NetBeans (6.5 as of this writing) comes with everything you need to develop and consume web services, including a choice of embedded web servers for development.

Step 2: Create a Web Project and Web Service Client

Next, create a new Web Project by clicking File > New and selecting Web Project from the list.

Once it builds your project, you must tell NetBeans what type of project you are developing.  You can do this by right clicking your newly created Project in the Projects pane and selecting New > Web Service Client .  If I remember correctly, this is where a wizard will prompt you for the Web Service WSDL that you are wanting to call .

WSDL stands for Web Service Definition Language and is a document that is generated when you build a web service to tell the applications that want to use it what methods are available to use.

Step 3: Call Web Service

Once your project finishes generating the local methods for calling the external web service, you should see a tree option under your project entitled Source Packages . Open this tree item and all items below it and you should see a Java file "*.java"Double click this so it opens, then right click > Web Service Client Resources > Call Web Service Operation .

Step 4: Modify and Test

Once the generation is completed, you should be able to reference the web service objects similar to the following:

WsNameOfService port =   service.getWsNameOfServicePort();
boolean tf;
tf = port.isReady();

out.println("Result: " + tf);

Enjoy and Good Luck!

Note: If your not getting the same menu items as me then you probably created the wrong project type in the first couple of steps.  For instance, NetBeans may not allow you to create a Web Service Client in a regular Java project.  Start over with a new project and ensure you are selecting the correct project types.