Sergei Sizov – Software Engineer Agile Software Development with Java and PHP

19Aug/090

Processing XML with Java

The de-facto standard for computer communication is XML. Java provides good support for XML processing. This tutorial shows how to read XML configuration files with Java. The source code used in this tutorial is taken from my Java application SimpleRSS Reader. I use XML format to store structured text data and to save program configuration.

Reading XML files

Example XML file: channels.xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<channels>
 <channel>
  <url>http://rss.dw-world.de/rdf/rss-en-world</url>
  <title>Deutsche Welle: DW-WORLD.DE - World</title>
  <updatestatus>OK</updatestatus>
  <updatetime>29.7.2009 21:41:16</updatetime>
 </channel>
 <channel>
  <url>http://rss.dw-world.de/rdf/rss-en-eu</url>
  <title>Deutsche Welle: DW-WORLD.DE - Europe</title>
  <updatestatus>OK</updatestatus>
  <updatetime>29.7.2009 21:41:15</updatetime>
 </channel>
</channels>

This is a configuration file that is used to store RSS channels. Each channel is represented by the Channel object that are stored in the channel ArrayList. When the application starts it parses the configuration file channels.xml and fills ArrayList<Channel> with Channel entities. This short tutorial is not about Collections, but there will be some extra code to make this example more interesting.

I use DocumentBuilder class to create DOM structure from XML. The DocumentBuilder is in javax.xml package, so to use it you need to have the following packages imported.

import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;
import org.w3c.dom.*;

The following code parses an XML file and loads the configuration.

// configuration XML file to store channels
File channelsFile = new File(userHomeDir + fileSeparator + programDirName +
                    fileSeparator + "channels.xml");
try {
// getting the instance of the DocumentBuilderFactory
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
// call factory method to get a new Document Builder instance
DocumentBuilder db = dbf.newDocumentBuilder();
// trying to parse the file
Document doc = db.parse(channelsFile);
// normalizing a file to merge all text nodes
doc.getDocumentElement().normalize();
// getting the list of <channel> nodes
NodeList nodeList = doc.getElementsByTagName("channel");
// loop through the node list
for (int i = 0; i < nodeList.getLength(); i++) {
// getting a node
Node channelNode = nodeList.item(i);
// an element found?
if (channelNode.getNodeType() == Node.ELEMENT_NODE) {
// strings to store values
String channelUrl = "";
String channelTitle = "";
// convert node to element
Element channelElement = (Element) channelNode;
// get Channel Url
NodeList channelUrlList = channelElement.getElementsByTagName("url");
Element channelUrlElement = (Element) channelUrlList.item(0);
NodeList channelUrlTextList = channelUrlElement.getChildNodes();
channelUrl = ((Node) channelUrlTextList.item(0)).getNodeValue().trim();
// getting the remaining parameters
// ...
// create a Channel instance
Channel aChannel = new Channel(channelUrl);
// configure the instance
aChannel.setName(channelTitle);
// add new Channel to ArrayList
channelList.add(aChannel);
} catch (Exception e) {
// catch exceptions ...
e.printStackTrace();
}
Tagged as: , Leave a comment
Comments (0) Trackbacks (0)

No comments yet.


Leave a comment


No trackbacks yet.

Pages

Categories

Blogroll

Archive

Meta