Android custom XML parser fails to resolve android namespace
Asked Answered
M

3

5

I would like to get the correct way of parsing custom android tags with an XmlResourceParser. I am using Eclipse 3.6 with the android plug-in, and I would like some attributes like the name be expanded with the full string from strings.xml.

Here is the index.xml which is being parsed in the res/xml/ folder.

<?xml version="1.0" encoding="utf-8"?>
<Index xmlns:android="http://schemas.android.com/apk/res/android">
<Sheet
    shortName="o_2sq"
    android:name="@string/o_2sq"
    instructions=""
/>
</Index>

Here is the file strings.xml in the res/values/ folder

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="o_2sq">Organize two squares</string>
</resources>

and the code fragment that parses the first index.xml with an XmlResourceParser:

String name = xpp.getAttributeValue(null, "android:name");
String shortName = xpp.getAttributeValue(null, "shortName");

The variable name contains null, but shortName contains "o_2sq". I also tried the following without success:

String name = xpp.getAttributeValue("android", "name");

What is the correct way of writing the sentence so that the variable name would contain "Organize two squares" ?

Marcille answered 12/4, 2012 at 9:8 Comment(0)
M
1

Best solution I found to overcome that problem.

String s = xpp.getAttributeValue("http://schemas.android.com/apk/res/android", "name");
String name = null;
if(s != null && s.length() > 1 && s.charAt(0) == '@') {
  int id = Integer.parseInt(s.substring(1));
  name = getString(id);
} else {
  name = "";
}
Marcille answered 11/5, 2012 at 6:49 Comment(2)
Did you mean else { name = s' } ? Seems like that would allow that attribute to be either a resource or a hard-coded string, no?Crampton
I needed name to be not null in my case. This is why.Tee
E
5

Try this:

String name = xpp.getAttributeValue("http://schemas.android.com/apk/res/android", "name");
Eyeshot answered 12/4, 2012 at 12:21 Comment(2)
Nice try. It gives something better : my variable name now contains the string "@2131099651". Not sure why.Tee
It is probably string resource ID. Try to use getString( number ); method to get string.Eyeshot
S
2
final String NAMESPACE_ANDROID = "http://schemas.android.com/apk/res/android";
final int VALUE_NOT_SET = -1;
int resId = parser.getAttributeResourceValue(NAMESPACE_ANDROID, "name", VALUE_NOT_SET);
String value = null;
if (VALUE_NOT_SET != resId) {
    value = context.getString(resId);
}

I think the above code could help you.

Supersonic answered 30/7, 2014 at 11:55 Comment(0)
M
1

Best solution I found to overcome that problem.

String s = xpp.getAttributeValue("http://schemas.android.com/apk/res/android", "name");
String name = null;
if(s != null && s.length() > 1 && s.charAt(0) == '@') {
  int id = Integer.parseInt(s.substring(1));
  name = getString(id);
} else {
  name = "";
}
Marcille answered 11/5, 2012 at 6:49 Comment(2)
Did you mean else { name = s' } ? Seems like that would allow that attribute to be either a resource or a hard-coded string, no?Crampton
I needed name to be not null in my case. This is why.Tee

© 2022 - 2024 — McMap. All rights reserved.