Hey there,
I am just a little confused with the code below, i parse the XML file although i cant call functions from outside the main. My programming isnt really up to scratch. Ive had a long break so now im paying for it :(
Any help/ suggestions would be very grateful!
package Util;
ALL IMPORTS DONE BY ECLIPSE
public class XMLreader{
public static void main(String argv[]){
try{
File file = new File("c:\\Programming Garmin Files\\2009-01-26-09-00-09.tcx");
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(file);
doc.getDocumentElement().normalize();
System.out.println("Root element: " + doc.getDocumentElement().getNodeName() + "\n");
NodeList nodeLst = doc.getElementsByTagName("Trackpoint");
//this works but i wish to call the functions from outside this class
readLatitude(nodeLst, 5);
}catch(Exception e){
e.printStackTrace();
}
}
public static float readLatitude(NodeList nodeLst, int index){
float latArr[] = new float[nodeLst.getLength()];
try{
for(int i=0; i<nodeLst.getLength(); i++){
Node fstNode = nodeLst.item(i);
if(fstNode.getNodeType() == Node.ELEMENT_NODE){
Element fstElmnt =(Element)fstNode;
NodeList latiElmntLst = fstElmnt.getElementsByTagName("LatitudeDegrees");
Element latiElmnt = (Element) latiElmntLst.item(0);
NodeList latitude = latiElmnt.getChildNodes();
latArr[i] = Float.valueOf(latitude.item(0).getNodeValue().trim()).floatValue();
System.out.println("Latitude Degrees :"+ latArr[i]);
}
}
}catch(NullPointerException e){
e.printStackTrace();
}
//System.out.println(latArr[index]);
return latArr[index];
}
public static float readLongitude(NodeList nodeLst, int index){
float longArr[] = new float[nodeLst.getLength()];
for(int i=0; i<nodeLst.getLength(); i++){
Node fstNode = nodeLst.item(i);
if(fstNode.getNodeType() == Node.ELEMENT_NODE){
Element fstElmnt =(Element)fstNode;
NodeList longElmntLst = fstElmnt.getElementsByTagName("LongitudeDegrees");
Element longElmnt = (Element) longElmntLst.item(0);
NodeList longitude = longElmnt.getChildNodes();
longArr[i] = Float.valueOf(longitude.item(0).getNodeValue().trim()).floatValue();
//System.out.println("Longitude Degrees :"+ longArr[i]);
}
}
return longArr[index];
}
public static float readAltitude(NodeList nodeLst, int index){
float altArr[] = new float[nodeLst.getLength()];
try{
for(int i=0; i<nodeLst.getLength(); i++){
Node fstNode = nodeLst.item(i);
if(fstNode.getNodeType() == Node.ELEMENT_NODE){
Element fstElmnt =(Element)fstNode;
NodeList altElmntLst = fstElmnt.getElementsByTagName("LongitudeDegrees");
Element altElmnt = (Element) altElmntLst.item(0);
NodeList altitude = altElmnt.getChildNodes();
altArr[i] = Float.valueOf(altitude.item(0).getNodeValue().trim()).floatValue();
//System.out.println("Altitude Degrees :"+ altArr[i]);
}
}
}catch(NullPointerException e){
e.printStackTrace();
}
return altArr[index];
}
public static void readDate(NodeList nodeLst){
for(int i= 0; i<nodeLst.getLength(); i++){
Node fstNode = nodeLst.item(i);
if(fstNode.getNodeType() == Node.ELEMENT_NODE){
Element fstElmnt =(Element)fstNode;
NodeList dateElmntLst = fstElmnt.getElementsByTagName("Time");
Element dateElmnt = (Element) dateElmntLst.item(0);
NodeList date = dateElmnt.getChildNodes();
//System.out.println("Time :"+((Node)date.item(0)).getNodeValue().trim());
String dateStr = ((Node)date.item(0)).getNodeValue().trim().toString();
String temp[]; temp = dateStr.split("T");
System.out.println("Date: " + temp[0] + " Time: " + temp[1]);
}
}
}
public static void readDistance(NodeList nodeLst){
for(int i= 0; i<nodeLst.getLength(); i++){
Node fstNode = nodeLst.item(i);
if(fstNode.getNodeType() == Node.ELEMENT_NODE){
Element fstElmnt =(Element)fstNode;
NodeList distElmntLst = fstElmnt.getElementsByTagName("DistanceMeters");
Element distElmnt = (Element) distElmntLst.item(0);
NodeList distance = distElmnt.getChildNodes();
float fDistance = Float.valueOf(distance.item(0).getNodeValue().trim()).floatValue();
System.out.println("Distance(meters) :"+ fDistance);
}
}
}
public float getLongitude(int index){
return readLongitude(nodeLst, index);
}
public float getLatitude(int index){
return readLatitude(nodeLst, index);
}
public float getAltitude(int index){
return readAltitude(nodeLst, index);
}
}
thanks again!