gpt4 book ai didi

java - Xmlparser.getText() 给出 null

转载 作者:行者123 更新时间:2023-11-29 21:11:06 27 4
gpt4 key购买 nike

我正在尝试从预设的 xml 文件中提取值,但当我尝试检查该值是什么时,我总是得到 null。

if (pulled.equals("preset")) {
presetName = xmlParser.getAttributeValue(null,"name");
Log.d(TAG, presetName + " = " + xmlParser.getText());
}

这是从中提取值的 xml im

<?xml version="1.0" encoding="utf-8"?>
<sports>
<sport name="Baseball" paid="false">
<preset name="Pitching Mound">726.0</preset>
<preset name="Base Distance">1080.0</preset>
</sport>
<sport name="Basketball" paid="false">
<preset name="NBA Free Throw Line">181.08</preset>
<preset name="NBA 3pt Line">265.8</preset>
</sport>
<sport name="Cricket" paid="true">
<preset name="Cricket Pitch">2012.0</preset>
<preset name="Testing">0.8</preset>
</sport>
</sports>

我做错了什么吗?

最佳答案

在 XmlPullParser api 上,getText() 方法具有以下描述:

Returns the text content of the current event as String. The value returned depends on current event type, for example for TEXT event it is element content (this is typical case when next() is used). See description of nextToken() for detailed description of
possible returned values for different types of events.

NOTE: in case of ENTITY_REF, this method returns the entity replacement text (or null if not available). This is the only case where getText() and getTextCharacters() return different values.

因此根据此描述,首先您必须检查当前 xml 节点是否为 TEXT,以便 getText() 不会返回 null。

if (pulled.equals("preset")) {
presetName = xmlParser.getAttributeValue(null,"name");
if (xmlParser.getEventType() == XmlPullParser.TEXT) {
Log.d(TAG, presetName + " = " + xmlParser.getText());
}
}

希望对您有所帮助,

关于java - Xmlparser.getText() 给出 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22803341/

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