A couple of years ago, I was asked to create an application that would monitor changes to program objects our Web application utilizes and would notify us when an object changed so that we could take appropriate action. Basically, this monitoring application runs and generates a report sent via e-mail. It also required the ability to produce an XML file, so it could be easily integrated with a Web front-end.
Well, being an XML newbie, I searched for a toolkit to get me started. I found several different packages that looked great, but The Mind Electric’s ElectricXML (EXML) was the most attractive. It's free and easy to use. Let’s take a closer look at what it can do for you.
How do I… ?
ElectricXML simplifies many facets of working with XML. Here are some of the more common tasks that the product makes easier.
Parse an XML document
Parsing a document using EXML is extremely simple. In Listing A the parseFile() method creates a Document object that accepts a File object in its constructor. From that point, just grab the root element using the getRoot() method and start iterating down the document to grab each child. My document has several attributes that I need to grab to implement the ProgramObject class. One thing to notice is the Elements object shown below. This object implements the Java Enumeration interface so that we may access all of the pgm elements using a collection. This is very handy, because it doesn't require a specific implementation on our part.
Elements elements = root.getElements( "pgm" );
Manipulate an XML document
Using the manipulate() method, the XML document is now reformatted to a different schema. The original intent of this was that we would receive several of the XML files from different clients and then merge them together to form a single report. The same steps apply as parsing: Create a document object; set the root element; and then add elements down the document as you go.
Write to an XML document
By far, writing to an XML document is easiest to do using EXML. Once you've created a document object with one line of code, you call the write() method using a file object again as the parameter.
(EXML has several other features that come with the package, and I encourage you to take a look at the examples included in the distribution.)
EXML in action
When I created my application, I set it up to produce a document with output that looked like the example in Listing B. If you notice, each pgm element has an id attribute, and several sub-elements. You may be wondering, “Why do you need an ID?” This is intentional, so that I can build a hashtable of program objects and use the ID as a key. You see, in the iSeries/AS400 world, you can have several objects named identically. Due to the nature of the iSeries, each object has an Object Type that sets the same-named objects apart from each other. This report is then parsed and put into another file on the server.
If your shop needs a very fast, simple—and let’s not forget, free—XML package, EXML is definitely worth the consideration. The package has removed some of the complexity of XML to make reading, writing, and manipulating XML much more efficient. Also, if you like what EXML has to offer, but want an even more robust collection of XML tools, I would encourage you to take a look at their commercial version.