I'm using javax.xml.stream.XMLInputFactory to parse an XML file, but I have trouble parsing the namespace.
In the first line of the XML a namespace is defined
<iati-activities xmlns:akvo="http://akvo.org/iati-activities" version="2.01" generated-datetime="2016-03-29T07:18:39Z">
I'm trying to decode using the following snippet
int theType = theReader.next();
switch (theType) {
case XMLStreamConstants.START_ELEMENT:
String theLocalName = theReader.getLocalName();
if (theLocalName.equalsIgnoreCase(IatiLabel.IATIACTIVITIES.getLabel())) {
for (int i = 0; i < theReader.getAttributeCount(); i++) {
System.out.println("Localname: " + theReader.getAttributeLocalName(i));
System.out.println("Namespace: " + i + " = " + theReader.getAttributeNamespace(i));
System.out.println("Attr value: " + theReader.getAttributeValue(i));
// System.out.println("Prefix: " + theReader.getNamespacePrefix(i));
System.out.println("URI: " + theReader.getNamespaceURI(i));
}
System.out.println("URI 2: " + theReader.getPrefix());
}
}
The result is:
Localname: version
Namespace: 0 = null
Attr value: 2.01
URI: http://akvo.org/iati-activities
Localname: generated-datetime
Namespace: 1 = null
Attr value: 2016-03-29T07:18:39Z
URI: null
URI 2:
If I un-comment the theReader.getNamespacePrefix() line I get a null pointer exception.
The rest of the XML file parses just fine, but I need to know the namespace prefix for a certain part.
Any idea what I'm doing wrong?