Android's XMLPullParser getAttributeValue is removing leading zeros from string

858 Views Asked by At

I have an XML document that contains strings with leading zeros. When I'm iterating the XML file with XmlResourceParser I have noticed that strings with leading zeros are modified removing the leading zeros when calling getAttributeValue. This functionality worked in the past and I have only noticed after upgrading to Android Studio 3.x. Is there something special I need to do in order for "getAttributeValue" to preserve the leading zeros?

Here is a test XML file that I'm using:

<?xml version="1.0" encoding="UTF-8"?>
<FictionalSpies>
<Property Country="Great Britain" Agency="MI6">
    <Item FullName="James Bond" AgentCode="007" />
    <Item FullName="John Wolfgramm" AgentCode="0010" />
    <Item FullName="Sam Johnston" AgentCode="0012" />
</Property>
<Property Country="United States" Agency="CONTROL">
    <Item FullName="Maxwell Smart" AgentCode="86" />
    <Item FullName="Unknown" AgentCode="99" />
    <Item FullName="The Chief" AgentCode="Q" />
</Property>
<Property Country="United States" Agency="MiB">
    <Item FullName="James Darrell Edwards III" AgentCode="J" />
    <Item FullName="Kevin Brown" AgentCode="K" />
    <Item FullName="Derrick Cunningham" AgentCode="D" />
</Property>
</FictionalSpies>

Here is the log print out for each 'spy' in the list. As you can see the first three have lost the "00" in their AgentCode. For example, James Bond should have agent code "007" and not "7".

D/XMLTest: Great Britain MI6 James Bond 7
D/XMLTest: Great Britain MI6 John Wolfgramm 10
D/XMLTest: Great Britain MI6 Sam Johnston 12
D/XMLTest: United States CONTROL Maxwell Smart 86
D/XMLTest: United States CONTROL Unknown 99
D/XMLTest: United States CONTROL The Chief Q
D/XMLTest: United States MiB James Darrell Edwards III J
D/XMLTest: United States MiB Kevin Brown K
D/XMLTest: United States MiB Derrick Cunningham D

Here is the code that is hooked up to a button press on a form and iterates the XML producing the previous log messages:

public void buttonOnClick(View v)
{
    int eventType = -1;
    String name;
    String country = null;
    String agency = null;
    String fullName = null;
    String agentCode = null;

    try
    {
        XmlResourceParser xmlRP = getResources().getXml(R.xml.test);

        while (eventType != XmlResourceParser.END_DOCUMENT)
        {
            if (eventType == XmlResourceParser.START_TAG)
            {
                name = xmlRP.getName();
                if (name.contentEquals("Property"))
                {
                    country = xmlRP.getAttributeValue(null, "Country");
                    agency = xmlRP.getAttributeValue(null, "Agency");
                } else if (name.contentEquals("Item"))
                {
                    fullName = xmlRP.getAttributeValue(null, "FullName");
                    agentCode = xmlRP.getAttributeValue(null, "AgentCode");

                    Log.d("XMLTest", country + " " + agency + " " + fullName + " " + agentCode );
                }
            }
            eventType = xmlRP.next();
        }
    } catch (XmlPullParserException e)
    {

    } catch (IOException e)
    {
    }
}
2

There are 2 best solutions below

1
Hemant Parmar On

XmlResourceParser provide all data types return value, you are getting int Agencycode from parser value. so instead of getAttributeValue() use getAttributeIntValue("namespace", "attribute",0) just replace this

agentCode = xmlRP.getAttributeValue(null, "AgentCode");

To

agentCode =xmlRP.getAttributeIntValue(null, "AgentCode",0);

Happy coding!!

0
timbru31 On

This is a bug in AAPT2. It's been reported and already fixed in Android Studio 3.4 and 3.5 (see https://androidstudio.googleblog.com/2019/01/android-studio-35-canary-2-available.html).

You need to upgrade the Android Gradle Plugin (AGP), too (as seen here: https://stackoverflow.com/a/35272475)

With the following build.gradle entry the AAPT2 behavior can be altered to keep leading zeros:

android {
    aaptOptions {
        additionalParameters "--keep-raw-values"
    }
}

Please note that this might increases your APK size.