Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/398.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/3/sql-server-2005/2.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:如何使用stax从xml获取特定信息_Java_Xml_Stax - Fatal编程技术网

Java:如何使用stax从xml获取特定信息

Java:如何使用stax从xml获取特定信息,java,xml,stax,Java,Xml,Stax,我在使用stax从xml中获取特定信息时遇到困难。在我的xml中,我有一个当前值和平均值,它们都有一个旅行时间。但我只想从当前数据中得到旅行时间,而不是平均值。以下是我的xml的外观: <allroutes> <routes> <route identification="id_1"> <current> <traveltime time="1187" t

我在使用stax从xml中获取特定信息时遇到困难。在我的xml中,我有一个当前值和平均值,它们都有一个旅行时间。但我只想从当前数据中得到旅行时间,而不是平均值。以下是我的xml的外观:

<allroutes>
    <routes>    
        <route identification="id_1">
            <current>
                <traveltime time="1187" trustworthy="j" />
                <delay time="0" trustworthy="j" />
            </current>
            <average>
                <traveltime time="1187" trustworthy="j" />
                <delay time="0" trustworthy="j" />
            </average>
        </route>
        <route identification="id_2">
            <current>
                <traveltime time="995" trustworthy="j" />
                <delay time="0" trustworthy="j" />
            </current>
            <average>
                <traveltime time="995" trustworthy="j" />
                <delay time="0" trustworthy="j" />
            </average>
        </route>
    </routes>
    <subpaths>
        <subpath identification="id_1">
            <current>
                <traveltime time="0" trustworthy="n" />
                <delay time="0" trustworthy="n" />
            </current>
            <average>
                <traveltime time="0" trustworthy="n" />
                <delay time="0" trustworthy="n" />
            </average>
        </subpath>
        <subpath identification="id_2">
            <current>
                <traveltime time="0" trustworthy="n" />
                <delay time="0" trustworthy="n" />
            </current>
            <average>
                <traveltime time="0" trustworthy="n" />
                <delay time="0" trustworthy="n" />
            </average>
        </subpath>
    </subpaths>
</allroutes>

我需要添加/更改什么才能从读卡器中的“当前”获取旅行时间?

您只需跟踪您在文档中的位置即可。例如,通常的做法是保留一个元素名堆栈:当您点击startElement时,将名称推到堆栈上,当您点击endElement时,将其弹出,然后检查堆栈以发现当前正在处理的元素的上下文。

Thank添加了一个列表,该列表在元素开始或结束时添加或删除localname。
try{
    while (streamReader.hasNext()) {
        streamReader.next();
        if (streamReader.getEventType() == XMLStreamReader.START_ELEMENT) {

            switch (streamReader.getLocalName()) {
            case "route":
                //store which type
                break;
            case "subpath":
                //store which type
                break;
            case "traveltime":
                //store traveltime
                break;
            }
        }

        if (streamReader.getEventType() == XMLStreamReader.END_ELEMENT && "allroutes".equals(streamReader.getLocalName())) {

            //stop the loop and give back the object
        }
    }
}catch(XMLStreamException ex) {
    LOGGER.log(Level.SEVERE, "XMLStreamException: " + ex);
}