简体   繁体   English

java和xml,使用Element

[英]java and xml, use of Element

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. 我是Java和XML的初学者,但是我有一个要执行的任务,而且我不知道如何使用使用Element的递归函数。 I made this program. 我做了这个程序。

public class JDOM2 { 公共类JDOM2 {
private static final String xmlroutes = "C://Users//Thomas//Desktop//routes.xml"; 私有静态最终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. 我需要两个函数,一个遍历所有根元素,并在起始STN处开始树遍历,还有一个递归函数,遍历树直到找到结束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.

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. 每次遇到非STN元素时, child元素都将作为current元素传递给recursive方法。 You will have to call this method using the root element as the root parameter and the same element as the current parameter. 您将必须使用root元素作为root参数并使用与current参数相同的元素来调用此方法。 ie, recursive(root, 0, root) . 即, recursive(root, 0, root)

Please note that I could not test this as the xml file is not accessible to me. 请注意,由于无法访问xml文件,因此无法对此进行测试。 Hope this helps. 希望这可以帮助。

For those who can be interested, I used this function to re-use the string "next.getAttributeValue("NAME")" as an element. 对于可能感兴趣的人,我使用此函数将字符串“ next.getAttributeValue(“ NAME”)“重新用作元素。

 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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM