我有一个XML文件(摘自它):
<regions_list>
<region type="continent" name="europe"
translate="=Europe" inner_download_suffix="europe" boundary="no" poly_extract="europe">
<!-- north-europe -->
<region name="denmark" lang="da" poly_extract="north-europe"/>
<region name="estonia" lang="et" poly_extract="north-europe"/>
<region name="iceland" lang="is" poly_extract="north-europe"/>
<region name="faroe-islands" translate="Faroe Islands;entity=node" lang="fo" poly_extract="north-europe"/>
<region name="finland" inner_download_prefix="$name" srtm="no" lang="fi,sv" poly_extract="north-europe">
<region map="no" name="aland" translate="name:fi=Ahvenanmaa;admin_level=3;entity=relation"/>
<region type="srtm" name="eastern-finland" translate="Eastern Finland;entity=relation"/>
<region name="northern-finland" map="no" srtm="no" hillshade="no" wiki="no" translate="name:en=Northern Finland;entity=relation" boundary="no">
<region type="srtm" name="lapland" translate="Lapland;entity=relation"/>
<region type="srtm" name="northern-ostrobothnia" translate="name:en=Northern Ostrobothnia;entity=relation"/>
</region>
<region type="srtm" name="southern-finland" translate="Southern Finland;entity=relation"/>
<region type="srtm" name="western-finland" translate="Western Finland;entity=relation"/>
</region>
<region name="latvia" lang="lv" poly_extract="north-europe"/>
<region name="lithuania" lang="lt" poly_extract="north-europe"/>
<region name="norway" srtm="no" inner_download_prefix="$name" lang="nb,nn" poly_extract="north-europe" join_map_files="yes">
<region srtm="yes" hillshade="no" wiki="no" name="akershus"/>
<region srtm="yes" hillshade="no" wiki="no" name="astfold" translate="Østfold;entity=node"/>
<region srtm="yes" hillshade="no" wiki="no" name="aust-agder"/>
<region srtm="yes" hillshade="no" wiki="no" name="buskerud"/>
<region srtm="yes" hillshade="no" wiki="no" name="finnmark"/>
<region srtm="yes" hillshade="no" wiki="no" name="hedmark"/>
<region srtm="yes" hillshade="no" wiki="no" name="hordaland"/>
<region srtm="yes" hillshade="no" wiki="no" name="more-og-romsdal" translate="Møre og Romsdal;entity=node"/>
<region srtm="yes" hillshade="no" wiki="no" name="nordland"/>
<region srtm="yes" hillshade="no" wiki="no" name="nord-trondelag" translate="Nord-Trøndelag;entity=node"/>
<region srtm="yes" hillshade="no" wiki="no" name="oppland"/>
<region srtm="yes" hillshade="no" wiki="no" name="oslo"/>
<region srtm="yes" hillshade="no" wiki="no" name="rogaland"/>
<region srtm="yes" hillshade="no" wiki="no" name="sogn-og-fjordane" translate="Sogn og Fjordane;entity=node"/>
<region srtm="yes" hillshade="no" wiki="no" name="sor-trondelag" translate="Sør-Trøndelag;entity=node"/>
<region srtm="yes" hillshade="no" wiki="no" name="telemark"/>
<region srtm="yes" hillshade="no" wiki="no" name="troms"/>
<region srtm="yes" hillshade="no" wiki="no" name="vest-agder"/>
<region srtm="yes" hillshade="no" wiki="no" name="vestfold"/>
<region name="svalbard-and-jan-mayen" translate="Svalbard;entity=relation"/>
</region>
<region name="sweden" inner_download_prefix="$name" srtm="no" lang="sv" poly_extract="north-europe">
<region map="yes" srtm="yes" hillshade="no" wiki="no" name="blekinge" translate="Blekinge län;entity=relation"/>
<region map="yes" srtm="yes" hillshade="no" wiki="no" name="dalarna" translate="Dalecarlia;entity=relation"/>
<region map="yes" srtm="yes" hillshade="no" wiki="no" name="gavleborg" translate="Gävleborgs län;entity=relation"/>
<region map="yes" srtm="yes" hillshade="no" wiki="no" name="gotland" translate="Gotlands län;entity=relation"/>
<region map="yes" srtm="yes" hillshade="no" wiki="no" name="halland" translate="Hallands län;entity=relation"/>
<region map="yes" srtm="yes" hillshade="no" wiki="no" name="jamtland" translate="Jämtlands län;entity=relation"/>
<region map="yes" srtm="yes" hillshade="no" wiki="no" name="jonkoping" translate="Jönköpings län;entity=relation"/>
<region map="yes" srtm="yes" hillshade="no" wiki="no" name="kalmar" translate="Kalmar län;entity=relation"/>
<region map="yes" srtm="yes" hillshade="no" wiki="no" name="kronoberg" translate="Kronobergs län;entity=relation"/>
<region map="yes" srtm="yes" hillshade="no" wiki="no" name="norrbotten" translate="Norrbottens län;entity=relation"/>
<region map="yes" srtm="yes" hillshade="no" wiki="no" name="orebro" translate="Örebro län;entity=relation"/>
<region map="yes" srtm="yes" hillshade="no" wiki="no" name="ostergotland" translate="Östergötlands län;entity=relation"/>
<region map="yes" srtm="yes" hillshade="no" wiki="no" name="skane" translate="Skåne län;entity=relation"/>
<region map="yes" srtm="yes" hillshade="no" wiki="no" name="sodermanland" translate="Södermanlands län;entity=relation"/>
<region map="yes" srtm="yes" hillshade="no" wiki="no" name="stockholm" translate="Stockholms län;entity=relation"/>
<region map="yes" srtm="yes" hillshade="no" wiki="no" name="uppsala" translate="Uppsala län;entity=relation"/>
<region map="yes" srtm="yes" hillshade="no" wiki="no" name="varmland" translate="Värmlands län;entity=relation"/>
<region map="yes" srtm="yes" hillshade="no" wiki="no" name="vasterbotten" translate="Västerbottens län;entity=relation"/>
<region map="yes" srtm="yes" hillshade="no" wiki="no" name="vasternorrland" translate="Västernorrlands län;entity=relation"/>
<region map="yes" srtm="yes" hillshade="no" wiki="no" name="vastmanland" translate="Västmanlands län;entity=relation"/>
<region map="yes" srtm="yes" hillshade="no" wiki="no" name="vastra-gotaland" translate="Västra Götalands län;entity=relation"/>
</region>
<!-- east-europe -->
<region name="belarus" lang="be,ru" poly_extract="east-europe"/>
<region name="bulgaria" lang="bg" poly_extract="east-europe"/>
<region name="czech-republic" inner_download_prefix="$name" translate="Czech Republic;entity=node" lang="cs,sk" poly_extract="east-europe" join_map_files="yes">
<region name="jihovychod" srtm="no" hillshade="no" wiki="no" translate="name:en=Southeast;ref=CZ06;entity=relation"/>
<region name="jihozapad" srtm="no" hillshade="no" wiki="no" translate="name:en=Southwest;ref=CZ03;entity=relation"/>
<region name="moravskoslezsko" srtm="no" hillshade="no" wiki="no" translate="name:en=Moravia-Silesia;entity=relation"/>
<region name="praha" srtm="no" hillshade="no" wiki="no" translate="name:en=Prague;entity=relation"/>
<region name="severovychod" srtm="no" hillshade="no" wiki="no" translate="name:en=Northeast;ref=CZ05;entity=relation"/>
<region name="severozapad" srtm="no" hillshade="no" wiki="no" translate="name:en=Northwest;ref=CZ04;entity=relation"/>
<region name="stredni-cechy" srtm="no" hillshade="no" wiki="no" translate="name:en=Central Bohemia;entity=relation"/>
<region name="stredni-morava" srtm="no" hillshade="no" wiki="no" translate="name:en=Central Moravia;entity=relation"/>
</region>
<region name="hungary" lang="hu" poly_extract="east-europe"/>
<region name="moldova" lang="ro" poly_extract="east-europe"/>
<region name="poland" inner_download_prefix="$name" lang="pl" poly_extract="east-europe" join_map_files="yes">
<region name="greater-poland" srtm="no" hillshade="no" wiki="no" translate="Greater Poland;entity=node"/>
<region name="kuyavian-pomeranian" srtm="no" hillshade="no" wiki="no" translate="Kuyavian-Pomerania;entity=node"/>
<region name="lesser-poland" srtm="no" hillshade="no" wiki="no" translate="Lesser Poland;entity=node"/>
<region name="lodz" srtm="no" hillshade="no" wiki="no" translate="Łódź;entity=node"/>
<region name="lower-silesian" srtm="no" hillshade="no" wiki="no" translate="Lower Silesia;entity=node"/>
<region name="lublin" srtm="no" hillshade="no" wiki="no" translate="name:en=Lublin Voivodeship;entity=relation"/>
<region name="lubusz" srtm="no" hillshade="no" wiki="no"/>
<region name="masovian" srtm="no" hillshade="no" wiki="no" translate="Masovia;entity=node"/>
<region name="opole" srtm="no" hillshade="no" wiki="no" translate="Opole Voivodeship;entity=node"/>
<region name="podlachian" srtm="no" hillshade="no" wiki="no" translate="Podlachia;entity=node"/>
<region name="pomeranian" srtm="no" hillshade="no" wiki="no" translate="Pomerania;entity=node"/>
<region name="silesian" srtm="no" hillshade="no" wiki="no" translate="Silesia;entity=node"/>
<region name="subcarpathian" srtm="no" hillshade="no" wiki="no" translate="Subcarpathia;entity=node"/>
<region name="swietokrzyskie" srtm="no" hillshade="no" wiki="no" translate="Holy Cross;entity=node"/>
<region name="warmian-masurian" srtm="no" hillshade="no" wiki="no" translate="Warmian-Masurian Voivodeship;entity=node"/>
<region name="west-pomeranian" srtm="no" hillshade="no" wiki="no" translate="West Pomerania;entity=node"/>
</region>
我尝试提取欧洲地区的childNodes的所有'name'属性(<region type="continent" name="europe"...>
,但我只得到前5个元素的名称。如果这些国家有childNodes,我也需要提取它们,但我现在的主要目标是国家。名称属性为'finland'的区域本身有childNodes,但是解析器查看名字=“aland”的第一个子节点并完全打破了循环。
public static Region parsedXMLForEurope(String xmlFile) {
List<Region> allRegions = new ArrayList<>();
Region region = new Region("Europe");
try {
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
XmlPullParser parser = factory.newPullParser();
parser.setInput(new StringReader(xmlFile));
int eventType = parser.getEventType();
while(eventType != XmlPullParser.END_DOCUMENT) {
Log.i("XMLParser", "******************");
Log.i("XMLParser", "Current counrtry is: " + parser.getAttributeValue(null, "name"));
switch (eventType) {
case XmlPullParser.START_TAG:
if(parser.getName().equals("region") && parser.getAttributeValue(null, "poly_extract").contains("europe")) {
Log.i("XMLParser", "******************");
Log.i("XMLParser", "Current country is: " + parser.getAttributeValue(null, "name"));
region.pushRegion(new Region(parser.getAttributeValue(null, "name")));
}
break;
case XmlPullParser.END_TAG:
Log.i("XMLParser", "******************");
Log.i("XMLParser", "Current country is: " + parser.getAttributeValue(null, "name"));
break;
}
eventType = parser.next();
}
} catch (Exception e ){
e.printStackTrace();
}
return region;
}
日志消息: LogMessages
答案 0 :(得分:0)
你有没有尝试过:
JSONObject jsonObj = XML.toJSONObject(xmlFile);
JSONArray jsonArray = jsonObj.getJSONObject("region").getJSONArray("region");
JSONObject jsonObjFromJsonArray = jsonArray.toJSONObject(0);
JSONObject jsonArrayObjForCountryRegion = jsonObjFromJsonArray.getJSONObject("region");
然后你可以将它用作JSON对象..访问数据非常容易,而不是解析XML内容。