سؤال

I'm trying to pull values from a preset xml file and I keep getting null when I try to check what the value was.

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

This is the xml im Pulling the value from

<?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>

Am I doing something wrong?

هل كانت مفيدة؟

المحلول

On XmlPullParser api, the getText() method has the following description:

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.

So based on this description, first you have to check if the current xml node is TEXT in order that getText() doesn't return null.

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

Hope this helps,

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top