I'll try to be brief as possible. I am trying to process KML (XML) files using the XmlPullParser in my Android application. I've got it working fairly well, but am getting hung up with the namespaces.
I initiate my parser like so:
public void parse(FileInputStream in) throws XmlPullParserException, IOException {
try {
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
factory.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
XmlPullParser parser = factory.newPullParser();
parser.setInput(in, null);
parser.nextTag();
readKML(parser);
} finally {
in.close();
}
}
Next, the program steps through the various tag checks I set up, and properly returns all the values UNTIL it gets to the part of the XML that uses namespaces (xal):
<Folder>
<name>Family & Friends</name>
<description>Locations of family members and friends.</description>
<Placemark>
<name>Fred and Marthas House</name>
<description></description>
<Point>
<coordinates>-55.55559,55.55555,120.0</coordinates>
</Point>
<xal:AddressDetails>
<xal:Country>
<xal:CountryName>United States</xal:CountryName>
<xal:CountryNameCode>US</xal:CountryNameCode>
<xal:AdministrativeArea>
<xal:AdministrativeAreaName>Michigan</xal:AdministrativeAreaName>
<xal:Locality>
<xal:LocalityName>Lawton</xal:LocalityName>
<xal:Thoroughfare>
<xal:ThoroughfareNumber></xal:ThoroughfareNumber>
<xal:ThoroughfareName>45th Street</xal:ThoroughfareName>
</xal:Thoroughfare>
<xal:PostalCode>
<xal:PostalCodeNumber></xal:PostalCodeNumber>
</xal:PostalCode>
</xal:Locality>
</xal:AdministrativeArea>
</xal:Country>
</xal:AddressDetails>
I process each individual XML value like this:
// Processes title tags in the feed.
private String readCountryNameCode(XmlPullParser parser) throws IOException, XmlPullParserException {
parser.require(XmlPullParser.START_TAG, NS, G.KML.COUNTRY_NAME_CODE);
String countryCode = readText(parser);
Log.d(TAG, "Found CountryNameCode value under Country tag");
parser.require(XmlPullParser.END_TAG, NS, G.KML.COUNTRY_NAME_CODE);
return countryCode;
}
Once it gets to the namespace portion of the xml it starts getting angry. The stacktrace says:
2018-12-24 12:59:43.026 1884-2314/OBFUSCATED_PERSONAL_INFO W/System.err: org.xmlpull.v1.XmlPullParserException: expected: START_TAG {null}CountryNameCode (position:END_TAG </{urn:oasis:names:tc:ciq:xsdschema:xAL:2.0}xal:Country>@30:21 in java.io.InputStreamReader@9f81207)
2018-12-24 12:59:43.027 1884-2314/OBFUSCATED_PERSONAL_INFO W/System.err: at org.kxml2.io.KXmlParser.require(KXmlParser.java:2067)
2018-12-24 12:59:43.027 1884-2314/OBFUSCATED_PERSONAL_INFO W/System.err: at OBFUSCATED_PERSONAL_INFO.KML_Import_Pull.readCountryNameCode(KML_Import_Pull.java:470)
So, next, I specified the namespace explicitly in the .require statement where the values are read:
// Processes title tags in the feed.
private String readCountryNameCode(XmlPullParser parser) throws IOException, XmlPullParserException {
parser.require(XmlPullParser.START_TAG, "xal", G.KML.COUNTRY_NAME_CODE);
String countryCode = readText(parser);
Log.d(TAG, "Found CountryNameCode value under Country tag");
parser.require(XmlPullParser.END_TAG, "xal", G.KML.COUNTRY_NAME_CODE);
return countryCode;
}
The stacktrace changed slightly, but is still more or less the same error:
2018-12-24 13:12:48.615 3842-4769/OBFUSCATED_PERSONAL_INFO W/System.err: org.xmlpull.v1.XmlPullParserException: expected: START_TAG {xal}CountryNameCode (position:START_TAG <{urn:oasis:names:tc:ciq:xsdschema:xAL:2.0}xal:CountryNameCode>@16:29 in java.io.InputStreamReader@eaf421)
2018-12-24 13:12:48.615 3842-4769/OBFUSCATED_PERSONAL_INFO W/System.err: at org.kxml2.io.KXmlParser.require(KXmlParser.java:2067)
2018-12-24 13:12:48.615 3842-4769/OBFUSCATED_PERSONAL_INFO W/System.err: at OBFUSCATED_PERSONAL_INFO.KML_Import_Pull.readCountryNameCode(KML_Import_Pull.java:470)
My only other thought is that the XML is malformed, but other applications process it just fine, and at least the first several entries appear well formed. I went through them manually as well as using another application to double check. edit I used an online xml validator and it didn't give any errors while checking the document
Did I miss anything setting up the XmlPullParser factory preparing it for this xal namespace?
The problem was programmatic, I had two erroneously placed parser.nextTag further up the tree that caused the errors.