Java: 如何使用 stax 从 xml 获取特定信息
Java: How to get specific information from xml using stax
我无法使用 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>
我目前的代码是这样的:
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);
}
我需要添加/更改什么才能仅从 'current' 获取此 reader 中的旅行时间?
您只需跟踪您在文档中的位置。例如,保留元素名称堆栈的常见做法是:当您点击 startElement 时将名称压入堆栈,当您点击 endElement 时将其弹出,然后检查堆栈以发现您当前正在处理的元素的上下文.
我无法使用 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>
我目前的代码是这样的:
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);
}
我需要添加/更改什么才能仅从 'current' 获取此 reader 中的旅行时间?
您只需跟踪您在文档中的位置。例如,保留元素名称堆栈的常见做法是:当您点击 startElement 时将名称压入堆栈,当您点击 endElement 时将其弹出,然后检查堆栈以发现您当前正在处理的元素的上下文.