Monthly Archives: May 2010

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

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

First Post

This is my first post, ever. Never before blogged. Really never thought anyone would care to read what I have to say. Its still possible no one wants to read what I have to say but I’m going to write it anyway. There is no plan for a consistent topic so bear with me as this thing drifts around from topic to topic, possibly out of control at times.

To get started, I work in software at a Tier 1 Automotive Supplier. Actually I manage a team of software engineers that develop Airbag Controllers. I became interested in software back in college, not when I was like 2 years old like you usually hear. I’ve been programming in mostly C with the occasional C++. I’ve started learning Java and PHP.

About a year ago, I started running. I completed my first half-marathon last September after 16 weeks of training. It took me over 2 hours @10:00 min/mile. I have my next half-marathon in June, Dexter Ann Arbor Run and hope to break under 2 hours @9:00 min/mile.

So we’ll see how it goes from here.


Categories: Uncategorized | Leave a comment

Blog at