Java

Java Programming

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"?>
<Config>
    <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" />
    </Software>
</Config>

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);
doc.getDocumentElement().normalize();
// 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"))
		{
			tmp.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++)
	{
		...
		{
			tmp.fields.add(tmpField);
		}
		// Add the software node the config data
		cfgItems.add(tmp);
	}

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

Categories: Java | Leave a comment

My First Java Program

Being an embedded software engineer who has mostly written software in C/C++ (more C than C++), when the opportunity to work on a side project for a PC program I jumped at the chance. Given that my employer doesn’t have many Visual C++ license (the development environment I’m most familiar with), I decided that the program should be written in Java. We don’t have a need for multiple platform usage but Java being free to develop with, it fits my company’s budget :).

The program needs to parse a Motorola S-Record file and display data from the file. The data fields pulled from the file need to be displayed as different types of data. The fields will be displayed as either ASCII, Raw Hex (exactly as read from S-Record), or unsigned 32-bit data. The only difference between Raw Hex and unsigned 32-bit data is that the 32-bit data is prefixed with “0x”.

There will be a variable number of fields read from each S-Record. The current plan is to the store the data fields in a config file as an XML file. Next installment, parsing the XML file.

Categories: Java | Leave a comment

Blog at WordPress.com.