XmlPullParser can't find attribute with a colon in its name

243 views Asked by At

In my XML there's a tag with an attribute which has a name with a colon in it:

<GGS:bericht StUF:bestandsnaam="bestand.txt" >

I've tried all these combinations to try and return the value of this attribute:

parser.getAttributeValue(null, "StUF:bestandsnaam");
parser.getAttributeValue("StUF", "bestandsnaam");
parser.getAttributeValue(null, "bestandsnaam");
parser.getAttributeValue("bestandsnaam", "StUF");

... but they all return null.

If I manually remove the "StUF:" part of the attribute name, it works by calling:

parser.getAttributeValue(null, "bestandsnaam");

So how do you get the value of such an attribute? Without using the int-parameter version of getAttributeValue(), that is.

2

There are 2 answers

0
Ian Roberts On BEST ANSWER

The "name with a colon in it" means the attribute is in a namespace. Somewhere further up in the XML document you should find a namespace declaration on one of the ancestors of this element that looks like

xmlns:StUF="{something}"

and it's this {something} (which will probably look like either an HTTP URL or a urn:...) that you need to pass as the "namespace" parameter. For example, if you had:

<root xmlns:GSS="urn:example:GSS" xmlns:StUF="http://stuff.com/namespace">
  <GGS:bericht StUF:bestandsnaam="bestand.txt" >

then the code would need to be

parser.getAttributeValue("http://stuff.com/namespace", "bestandsnaam");
0
fejese On

The documentation for getAttributeValue with the namespace parameter says you need to enable namespace processing. Try enabling it at the beginning of the processing as in the example for setFeature

setFeature(FEATURE_PROCESS_NAMESPACES, true)