XML Processing In Java Using XOM
I have decided to use XOM API to process XML data. There is no good reason actually. I may revisit another XML API (e.g. JDOM) in the future.
What I have done is to read the XML from the Internet (e.g. Digg RSS) and process it.
To summarize what I have done:
1. Open a connection.
2. Tell the XML API to read from the connection stream.
Simple, eh? ![]()
Let’s go into more detailed setup…
We can get the XOM API from the following website: XOM website. In particular, get the Jar file. Put the file in your (project directory)/lib.
For Eclipse folks out there (I love Eclipse!), you may do the following steps:
1. You should see xom-1.1.jar in your /lib directory. If you don’t see the xom-1.1.jar, you should refresh your project (right-click and select Refresh).
3. Right-click on the Jar file.
4. Select Build Path and Add to Build Path.
To get the code simpler, I have removed the error handling and log4j code.
import java.net.*;
import nu.xom.*;
import java.io.*;
URL url = null;
URLConnection urlConn = null;
InputStream inpStream = null;
Builder builder = null;
Document doc = null;
//Try process the URL
url = new URL(request);
//Open connection
urlConn = url.openConnection();
//Get the InputStream
inpStream = urlConn.getInputStream();
//Tell the Document to process the input stream
builder = new Builder();
doc = builder.build(inpStream);
//Get the XML now
xmlString = doc.toXML(); //xmlString is defined as the String type