i'm a beginner in java and XML , but I have a task to perform and I don't know how to use a recursive function which uses Element. I made this program.
public class JDOM2
{
private static final String xmlroutes = "C://Users//Thomas//Desktop//routes.xml";
static Element root;
public static void main(String[] args) throws JDOMException, IOException
{
// the SAXBuilder is the easiest way to create the JDOM2 objects.
SAXBuilder jdomBuilder = new SAXBuilder();
// jdomDocument is the JDOM2 Object
Document jdomDocument = jdomBuilder.build(xmlroutes);
root = jdomDocument.getRootElement();
List <Element> location_properties = root.getChildren("LOCATION_PROPERTIES");
Iterator<Element> it = location_properties.iterator();
Element loc = it.next();
rootiteration();
}
public static void rootiteration()
{
int time;
List <Element> location_properties = root.getChildren("LOCATION_PROPERTIES");
Iterator<Element> it = location_properties.iterator();
Element loc = it.next();
if(loc.getAttributeValue("NAME").startsWith("STN")== true)
{
List <Element> segment_properties = loc.getChildren("SEGMENT_PROPERTIES");
Iterator<Element> it2 = segment_properties.iterator();
Element seg = it2.next();
List <Element> next_location = seg.getChildren("NEXT_LOCATION");
for (Element next: next_location)
{
if(next.getAttributeValue("NAME").startsWith("STN")== true)
{
System.out.print("Arrival : " +next.getAttributeValue("NAME"));
int L = Integer.parseInt(next.getAttributeValue("LENGTH"));
int S = Integer.parseInt(next.getAttributeValue("SPEED"));
time = L/S;
System.out.println(" --- Time to go : "+time+" seconds");
}
if(next.getAttributeValue("NAME").startsWith("STN")== false)
{
recursive(); // I think the problem is here but I may have done some other mistakes.
}
}
}
}
public static int recursive(Element parent, int t0, Element child) throws IOException
{
List <Element> location_properties = root.getChildren("LOCATION_PROPERTIES");
Iterator <Element> i = location_properties.iterator();
int t1 = 0;
while (i.hasNext())
{
child = (Element) i.next();
int L = Integer.parseInt(child.getChild("SEGMENT_PROPERTIES").getChild("NEXT_LOCATION").getAttributeValue("LENGTH"));
int S = Integer.parseInt(child.getChild("SEGMENT_PROPERTIES").getChild("NEXT_LOCATION").getAttributeValue("SPEED"));
t1 = L/S;
//t1 = time_between();
if (child.getAttributeValue("NAME").startsWith("STN")== true)
{
System.out.println("From : "+parent+" to "+child+" --- Time to go : "+t1+" seconds");
System.out.println(child.getAttributeValue("NAME"));
System.out.println(parent);
}
if (child.getAttributeValue("NAME").startsWith("X")== true) // child is not STN, recurse
{
recursive(parent, t0 + t1,child);
System.out.println("From : "+parent+" to "+child+" --- Time to go : "+t1+" seconds");
// t0 = t0 + t1;
}
}
return t0;
}
This is supposed to calculate the time between 2 Elements. King of this way :
I need two functions, one which iterates over all root elements, and starts the tree traversal at a starting STN, and a recursive function that traverse the tree until it finds an ending STN.
To have something like that :
Departure Station : STN10
Arrival : X535 --- Time to go : 16 seconds
Arrival : X536 --- Time to go : 2 seconds
Arrival : X537 --- ...
Arrival : STN26 --- Total time to Station : ...
Departure Station : STN11
Arrival : X535 ---
...And so on.