在解析XML时,如何在Android中使用XmlPullParser正确处理名称空间?

时间:2018-12-24 18:21:57

标签: android xml kml xmlpullparser

我会尽量简短。我正在尝试使用Android应用程序中的XmlPullParser处理KML(XML)文件。我已经很好地工作了,但是却迷上了名称空间。

我这样启动解析器:

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();
    }
}

接下来,该程序逐步完成我设置的各种标记检查,并正确返回所有值,直到将其返回到使用名称空间(xal)的XML部分:

<Folder>
            <name>Family &amp; 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>

我这样处理每个单独的XML值:

// 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;
}

一旦到达xml的名称空间部分,它就会开始生气。堆栈跟踪显示:

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)

因此,接下来,我在.require语句中显式指定了名称空间,并在其中读取了值:

// 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;
}

stacktrace稍有变化,但还是有相同的错误:

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)

我唯一的另一种想法是XML格式错误,但是其他应用程序对其进行了很好的处理,至少前几个条目看起来格式正确。我手动检查了它们,并使用另一个应用程序进行了仔细检查。 edit 我使用了在线xml validator,并且在检查文档时没有出现任何错误

我是否有任何设置XmlPullParser工厂的事情为这个xal名称空间做准备吗?

1 个答案:

答案 0 :(得分:0)

问题是程序性的,我将两个错误的parser.nextTag放置在导致错误的树上。