Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java和xml,元素的使用_Java_Xml_Recursion_Tree Traversal - Fatal编程技术网

java和xml,元素的使用

java和xml,元素的使用,java,xml,recursion,tree-traversal,Java,Xml,Recursion,Tree Traversal,我是java和XML的初学者,但我有一项任务要执行,我不知道如何使用使用元素的递归函数。 我做了这个节目 公共类JDOM2 { 私有静态最终字符串xmlroutes=“C://Users//Thomas//Desktop//routes.xml” 我认为您可以按如下方式更改第二种方法,它将起作用: public static int recursive(Element root, int t0, Element current) throws IOException { List &l

我是java和XML的初学者,但我有一项任务要执行,我不知道如何使用使用元素的递归函数。 我做了这个节目

公共类JDOM2 {
私有静态最终字符串xmlroutes=“C://Users//Thomas//Desktop//routes.xml”


我认为您可以按如下方式更改第二种方法,它将起作用:

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;
}
publicstaticintrecursive(元素根,intt0,元素当前)抛出IOException
{
List location_properties=current.getChildren(“location_properties”);
迭代器i=位置_属性。迭代器();
int t1=0;
while(i.hasNext())
{
child=(元素)i.next();
int L=Integer.parseInt(child.getChild(“段属性”).getChild(“下一个位置”).getAttributeValue(“长度”);
ints=Integer.parseInt(child.getChild(“段属性”).getChild(“下一个位置”).getAttributeValue(“速度”));
t1=L/S;
//t1=之间的时间_();
if(child.getAttributeValue(“NAME”).startsWith(“STN”)==true)
{
System.out.println(“从:“+root+”到“+child+”——运行时间:“+t1+”秒”);
System.out.println(child.getAttributeValue(“NAME”);
}
如果(child.getAttributeValue(“NAME”).startsWith(“X”)==true)//child不是STN,则递归
{
递归(根,t0+t1,子);
}
}
返回t0;
}
每次遇到非STN元素时,
子元素
将作为
当前
元素传递给
递归
方法。 您必须使用
root
元素作为
root
参数,并使用与
current
参数相同的元素来调用此方法。例如,
recursive(root,0,root)


请注意,我无法对此进行测试,因为我无法访问xml文件。希望这能有所帮助。

对于可能感兴趣的人,我使用此函数将字符串“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;
}
公共静态元素getElembyName(最终字符串名){
List location_properties=root.getChildren(“location_properties”);
对于(元素位置:位置和属性)
{
if(loc.getAttributeValue(“NAME”).equals(NAME))
返回loc;
}
返回null;
}

问题是什么?当我在“rootIteration”中调用递归函数时,如何使程序工作,我需要递归函数调用中的参数,但我不知道元素是如何工作的。如果您愿意,我可以向您发送一封包含该文件的电子邮件,或者尝试以下链接:它实际上不起作用。因为如果您查看main,元素根已经按您的方式使用,但效果不太好。另外,我不能在调用t时输入数字函数,它是一个必须存在的变量。
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;
}
 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;
}