I am trying to parse the following XML file in Java and to store the XTimeStamp variable in an ArrayList and YVoltage in another ArrayList:
<?xml version = "1.0"?>
<Data>
<reading>
<XTimeStamp> 12:00:00:01</XTimeStamp>
<YVoltage> 0.6</YVoltage>
</reading>
<reading>
<XTimeStamp> 12:00:00:02</XTimeStamp>
<YVoltage> 0.5</YVoltage>
</reading>
<reading>
<XTimeStamp> 12:00:00:025</XTimeStamp>
<YVoltage> 0.5</YVoltage>
</reading>
<reading>
<XTimeStamp> 12:00:00:031</XTimeStamp>
<YVoltage> 0.1</YVoltage>
</reading>
<reading>
<XTimeStamp> 12:00:00:039</XTimeStamp>
<YVoltage> -0.1</YVoltage>
</reading>
<reading>
<XTimeStamp> 12:00:00:050</XTimeStamp>
<YVoltage> -0.2</YVoltage>
</reading>
<reading>
<XTimeStamp> 12:00:01:01</XTimeStamp>
<YVoltage> 0.01</YVoltage>
</reading>
</Data>
Here is the Java code I currently have to accomplish the task:
import java.util.*;
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class XMLReader {
public static void main(String argv[]) {
ArrayList XTimeStamp = new ArrayList();
ArrayList YVoltage = new ArrayList();
try {
File file = new File("C:\\Users\\user\\My Documents\\MyXMLFile.xml");
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(file);
doc.getDocumentElement().normalize();
System.out.println("Root element "
+ doc.getDocumentElement().getNodeName());
NodeList nodeLst = doc.getElementsByTagName("reading");
System.out.println("Data");
for (int s = 0; s < nodeLst.getLength(); s++) {
Node fstNode = nodeLst.item(s);
if (fstNode.getNodeType() == Node.ELEMENT_NODE) {
Element fstElmnt = (Element) fstNode;
NodeList fstNmElmntLst = fstElmnt
.getElementsByTagName("XTimeStamp");
Element fstNmElmnt = (Element) fstNmElmntLst.item(0);
NodeList fstNm = fstNmElmnt.getChildNodes();
Element TimeStamp = (Element) fstNmElmntLst.item(0);
XTimeStamp.add(TimeStamp);
System.out.println("XTimeStamp : "
+ ((Node) fstNm.item(0)).getNodeValue());
NodeList lstNmElmntLst = fstElmnt
.getElementsByTagName("YVoltage");
Element lstNmElmnt = (Element) lstNmElmntLst.item(0);
NodeList lstNm = lstNmElmnt.getChildNodes();
Element YValue = (Element) lstNm;
YVoltage.add(YValue);
System.out.println("YVoltage : "
+ ((Node) lstNm.item(0)).getNodeValue());
}
}
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(XTimeStamp);
System.out.println(YVoltage);
}
}
When I run the code I am getting the following output, as can be seen from the listings of the ArrayLists at the bottom there is a problem:
Root element Data
Data
XTimeStamp : 12:00:00:01
YVoltage : 0.6
XTimeStamp : 12:00:00:02
YVoltage : 0.5
XTimeStamp : 12:00:00:025
YVoltage : 0.5
XTimeStamp : 12:00:00:031
YVoltage : 0.1
XTimeStamp : 12:00:00:039
YVoltage : -0.1
XTimeStamp : 12:00:00:050
YVoltage : -0.2
XTimeStamp : 12:00:01:01
YVoltage : 0.01
[[XTimeStamp: null], [XTimeStamp: null], [XTimeStamp: null], [XTimeStamp: null], [XTimeStamp: null], [XTimeStamp: null], [XTimeStamp: null]]
[[YVoltage: null], [YVoltage: null], [YVoltage: null], [YVoltage: null], [YVoltage: null], [YVoltage: null], [YVoltage: null]]
Any help with this would be much appreciated.
Thanks.