0

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.
Thonath
  • 13
  • 1
  • 4
  • What is the question? – Hirak May 20 '14 at 09:21
  • How can I make my program work? When I call the function recursive in "rootIteration", I need parameters in the call of the function recursive but I don't know how Element works. – Thonath May 20 '14 at 09:28

2 Answers2

0

I think you can change the second method as follows and it would work:

public static int recursive(Element root, int t0, Element current) throws IOException 
{
    List <Element> location_properties = current.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 : "+root+" to "+child+"  ---  Time to go : "+t1+" seconds");
            System.out.println(child.getAttributeValue("NAME")); 
        }
        if (child.getAttributeValue("NAME").startsWith("X")== true) // child is not STN, recurse
        {
            recursive(root, t0 + t1, child);
        }
    }
    return t0;
}

Everytime a non-STN element is encountered the child element will be passed to the recursive method as the current element. You will have to call this method using the root element as the root parameter and the same element as the current parameter. i.e., recursive(root, 0, root).

Please note that I could not test this as the xml file is not accessible to me. Hope this helps.

Priyesh
  • 2,041
  • 1
  • 17
  • 25
  • I can send you an email with the file if you want, or try this link : http://www.megafileupload.com/en/file/532301/routes-xml.html – Thonath May 20 '14 at 11:12
  • It isn't really working.. because if you look at the main, the element root is already used – Thonath May 20 '14 at 17:00
  • The way that you did it wasn't working very well. Plus I mustn't put a number in the call of the function, it's a variable which have to be in . – Thonath May 22 '14 at 09:15
0

For those who can be interested, I used this function to re-use the string "next.getAttributeValue("NAME")" as an element.

 public static Element getElembyName(final String name){
    List <Element> location_properties = root.getChildren("LOCATION_PROPERTIES");
    for (Element loc : location_properties)
    {
        if(loc.getAttributeValue("NAME").equals(name))
            return loc;
    }
    return null;
}
Thonath
  • 13
  • 1
  • 4