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)
{
}
}