Hi All,
I have the following code to read in an XML file and to get the basic statistical information. I am having problems trying to get it working. Here is what I have so far:
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 javaFile
{
public static void main(String argv[]) {
java.util.ArrayList timeStampArray = new java.util.ArrayList();
java.util.ArrayList XY = new java.util.ArrayList();
java.util.ArrayList PriceArray = new java.util.ArrayList();
try {
File file = new File("c:/Users/user/Desktop/Shares/shares.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("shareprice");
System.out.println("Company 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("timeStamp");
Element fstNmElmnt = (Element) fstNmElmntLst.item(0);
NodeList fstNm = fstNmElmnt.getChildNodes();
String timeStamp = fstNm.item(0).getNodeValue();
timeStampArray.add(timeStamp);
System.out.println("timeStamp : " + ((Node) fstNm.item(0)).getNodeValue());
NodeList lstNmElmntLst = fstElmnt.getElementsByTagName("Price");
Element lstNmElmnt = (Element) lstNmElmntLst.item(0);
NodeList lstNm = lstNmElmnt.getChildNodes();
String YValue = lstNm.item(0).getNodeValue();
PriceArray.add(YValue);
System.out.println("Price : " + ((Node) lstNm.item(0)).getNodeValue());
}
}
}
catch (Exception e) {
e.printStackTrace();
}
System.out.println(timeStampArray);
System.out.println(PriceArray);
XY.add (timeStampArray);
XY.add (PriceArray);
System.out.println(XY);
}
public double getAverage( int a[])
{
int count=0;
for( int i=0; i < a.size(); i++ )
{
count += a.get( i );
}
double result=0;
result = (double) count / (double) a.size();
return result;
}
public static double median(int a[]) {
int[] b = new int[a.length];
System.arraycopy(a, 0, b, 0, b.length);
Arrays.sort(b);
if (a.length % 2 = 0)
{
return (b[(b.length / 2) - 1] + b[b.length / 2]) / 2.0;
}
else
{
return b[b.length / 2];
}
}
public static int maxValue(int a[])
{
int maxValue, maxCount;
for (int i = 0; i < a.length; ++i) {
int count = 0;
for (int j = 0; j < a.length; ++j)
{
if (a[j] == a[i]) ++count;
}
if (count > maxCount)
{
maxCount = count;
maxValue = a[i];
}
}
return maxValue;
}
}
The XML File it reads in is as follows:
<Company >
<shareprice>
<timeStamp> 12:00:00.01</timeStamp>
<Price> 25.02</Price>
</shareprice>
<shareprice>
<timeStamp> 12:00:00.02</timeStamp>
<Price> 15</Price>
</shareprice>
<shareprice>
<timeStamp> 12:00:01.025</timeStamp>
<Price> 15.02</Price>
</shareprice>
<shareprice>
<timeStamp> 12:00:01.031</timeStamp>
<Price> 18.25</Price>
</shareprice>
<shareprice>
<timeStamp> 12:00:01.039</timeStamp>
<Price> 18.54</Price>
</shareprice>
<shareprice>
<timeStamp> 12:00:01.050</timeStamp>
<Price> 16.52</Price>
</shareprice>
<shareprice>
<timeStamp> 12:00:02.01</timeStamp>
<Price> 17.50</Price>
</shareprice>
<shareprice>
<timeStamp> 12:00:03.01</timeStamp>
<Price> 25.02</Price>
</shareprice>
<shareprice>
<timeStamp> 12:00:05.02</timeStamp>
<Price> 30</Price>
</shareprice>
<shareprice>
<timeStamp> 12:00:11.025</timeStamp>
<Price> 32.25</Price>
</shareprice>
<shareprice>
<timeStamp> 12:00:12.031</timeStamp>
<Price> 26.05</Price>
</shareprice>
<shareprice>
<timeStamp> 12:00:15.039</timeStamp>
<Price> 18.54</Price>
</shareprice>
<shareprice>
<timeStamp> 12:00:19.050</timeStamp>
<Price> 16.52</Price>
</shareprice>
<shareprice>
<timeStamp> 12:01:02.01</timeStamp>
<Price> 17.50</Price>
</shareprice>
</Company>
I would like to be able to read in that XML File, carry out the statistical analysis and output an XML file like so:
<graph caption="Share Data Wave" subcaption="For Person's Name" xAxisName="Time" yAxisMinValue="-0.025" yAxisName="Voltage" decimalPrecision="5" formatNumberScale="0" numberPrefix="" showNames="1" showValues="0" showAlternateHGridColor="1" AlternateHGridColor="ff5904" divLineColor="ff5904" divLineAlpha="20" alternateHGridAlpha="5">
<set name="2010-08-27 12:00:20.636" value="25.020000" hoverText = "The difference from last value: 0" ></set>
<set name="2010-08-27 12:01:19.473" value="15.000000" hoverText = "The difference from last value: -10.02" ></set>
<set name="2010-08-27 12:01:24.494" value="15.020000" hoverText = "The difference from last value: 0.0199999999999996" ></set>
<set name="2010-08-27 12:01:44.188" value="18.250000" hoverText = "The difference from last value: 3.23" ></set>
<set name="2010-08-27 12:02:11.851" value="18.540000" hoverText = "The difference from last value: 0.289999999999999" ></set>
<set name="2010-08-27 12:02:47.109" value="16.520000" hoverText = "The difference from last value: -2.02" ></set>
<set name="2010-08-27 12:03:01.199" value="17.500000" hoverText = "The difference from last value: 0.98" ></set>
<set name="2010-08-27 12:03:03.030" value="25.020000" hoverText = "The difference from last value: 7.52" ></set>
<set name="2010-08-27 12:03:40.570" value="30.000000" hoverText = "The difference from last value: 4.98" ></set>
<set name="2010-08-27 12:04:27.490" value="32.250000" hoverText = "The difference from last value: 2.25" ></set>
<set name="2010-08-27 12:05:03.738" value="26.050000" hoverText = "The difference from last value: -6.2" ></set>
<set name="2010-08-27 12:05:14.511" value="18.540000" hoverText = "The difference from last value: -7.51" ></set>
<set name="2010-08-27 12:06:09.728" value="16.520000" hoverText = "The difference from last value: -2.02" ></set>
<set name="2010-08-27 12:06:58.329" value="17.500000" hoverText = "The difference from last value: 0.98" ></set>
</graph>
<statistics>
<mean>20.8378571428571</mean>
<sd>5.69132021803165</sd>
<min>15</min>
<median>18.395</median>
<max>32.25</max>
</statistics>
I am stuck as how to do this from here!
Any and all help and suggestions will be greatly appreciated!