gpt4 book ai didi

java - 使用stax解析器仅从xml获取特定元素节点

转载 作者:行者123 更新时间:2023-11-29 05:48:36 25 4
gpt4 key购买 nike

<xml>
<name>steve</name>
<age>12</age>
<country>Germany</country>
<person>
<college>kmu university</college>
<country>Sweden</country>
</person>
</xml>

在解析时使用 stax 解析器,我得到的国家名称为德国、瑞典。但我只需要瑞典,不需要德国。任何其他获取它的方法。我正在使用 STAX 解析器解析此 xml。

预期产出:瑞典

实际产出:德国、瑞典

最佳答案

当您进行 StAX(和 SAX)解析时,您需要自己跟踪上下文。这意味着您需要跟踪已经开始但尚未完成的元素。获得此信息后,您就可以仅保留嵌套在“person”元素下的“country”元素,而忽略所有其他“country”元素。

关于java - 使用stax解析器仅从xml获取特定元素节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14878129/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com