Reading an XML file with Java

I’m sure there are many ways to read and parse XML files in Java. The XML file in question is as follows:

<?xml version="1.0" encoding="UTF-8"?>
    <Software offset="0x00000000" name="Bootloader" length="16384">
        <Field name="Program Id" offset="0x3FEE" length="14" type="String" />
        <Field name="Security Key" offset="0x3FE0" length="6" type="Hex" />
        <Field name="Checksum" offset="0x3FFC" length="4" type="u32" />

This was the first method I found to read the XML data without writing my own XML reader/parser. There are probably more elegant ways to do this but it works. The DocumentBuilderFactory and DocumentBuilder classes will require some more investigation but that will be the

// Open File
File file = new File(fname);

// Set the XML Parser
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(file);
// Root node is 'Config'
// Get all 'Software' Nodes
nodeLst = doc.getElementsByTagName("Software");

This reads in all the XML data into memory and provides a list of all “Software” Nodes. The data for each software node is stored in the nodes attributes. Now the software nodes will be looped through pulling out all the attributes and all the “Fields” that the “Software” contains.

// Loop through all software nodes to process data.
for (int s = 0; s < nodeLst.getLength(); s++)
	String tmpStr;
	SoftwareXMLData tmp = new SoftwareXMLData();

	// Get a reference to the node
	Node fstNode = nodeLst.item(s);
	// Get the attributes for the software node
	NamedNodeMap attrs = fstNode.getAttributes();

	for (int i = 0; i < attrs.getLength(); i++)
		Attr attribute = (Attr)attrs.item(i);
		// If it is the length attribute, convert to integer before saving
		if (attribute.getName().equals("length"))
			tmpStr = attribute.getValue();
			tmp.length = (int)Str2Int(tmpStr);
		// If it is the offset attribute, convert to integer before saving
		else if (attribute.getName().equals("offset"))
			tmpStr = attribute.getValue();
			tmp.offset = Str2Int(tmpStr);
		// If it is the name attribute, store the data.
		else if (attribute.getName().equals("name"))
		{ = attribute.getValue();

After saving the attribute for each “Software” node, loop through each field node by getting the “Software” node element followed by the list of nodes with the name “Field” (not a very descriptive name). Loop through these fields and get the attributes for each field similar to the “Software” field above. At the end of each loop, store the tmp data in an Arraylist

	// Get the 'Field' nodes
	Element fstElmnt = (Element) fstNode;
	NodeList fieldNode = fstElmnt.getElementsByTagName("Field");

	for (int i = 0; i < fieldNode.getLength(); i++)
		// Add the software node the config data

All and all a fairly simple process thanks to XML and Java – Parsing XML using Java Tutorial.

Categories: Java | Leave a comment

Post navigation

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

Blog at

%d bloggers like this: