我正在构建一个 android 应用程序,用户在其中根据他的搜索获取可能的城市列表。我已经使用 Google Places Api 来实现这一点,并且我正在使用 XMLPullParser 来解析收到的 xml。我尝试了以下代码
public ArrayList<String> getPlaceList(InputStream in) {
// TODO Auto-generated method stub
ArrayList<String> placesList = new ArrayList<String>();
XmlPullParserFactory factory;
try {
factory = XmlPullParserFactory.newInstance();
// allow xmlpullparserfactory to parse document as xml,default false
factory.setNamespaceAware(true);
XmlPullParser parser = factory.newPullParser();
// assign inputstream and no encoding
parser.setInput(in, null);
int eventType = parser.getEventType();
// until the document ends
while (eventType != XmlPullParser.END_DOCUMENT) {
Toast t = Toast.makeText( myContext, parser.getName(), Toast.LENGTH_LONG);
t.show();
if (eventType == XmlPullParser.START_TAG
&& parser.getName().equals("prediction")) {
eventType = parser.next();
while (!(eventType == XmlPullParser.END_TAG
&& parser.getName().equals("prediction"))) {
if (eventType == XmlPullParser.START_TAG
&& parser.getName().equals("description")) {
placesList.add(parser.nextText());
String text="text ";
}
eventType = parser.next();
}
}
eventType=parser.next();
}
return placesList;
} catch (Exception e) {
Toast t1 = Toast.makeText( myContext, "error xmlhandling", Toast.LENGTH_LONG);
t1.show();
}
return placesList;
}
这是 Google 地方信息
<?xml version="1.0" encoding="UTF-8"?>
<AutocompletionResponse>
<status>OK</status>
<prediction>
<description>Delhi, India</description>
<type>locality</type>
<type>political</type>
<type>geocode</type>
<reference>CjQkAAAAVhtWJ9i5zyacN4kIoR7-YN4MIVw0d14B9WIs7R4PQDPEV4B_C2lZqEPDxLlwHvn_EhCJZfrQNdx-1Bpug0gfs8j5GhSuqEbP4q_w5kniq5Hhora1pddvHg</reference>
<id>910e9ee78403c6726e140168f78ca0fb0063bcc8</id>
<term>
<value>Delhi</value>
<offset>0</offset>
</term>
<term>
<value>India</value>
<offset>7</offset>
</term>
<matched_substring>
<offset>0</offset>
<length>4</length>
</matched_substring>
</prediction>
<prediction>
<description>Delhi Cantonment, New Delhi, Delhi, India</description>
<type>sublocality</type>
<type>political</type>
<type>geocode</type>
<reference>ClRBAAAAldH2XsHxApFz9h9KT7EqFGQY9lX3fPbIQEvY2r4QX9CfMywdOa3do8D1TxG3QvPDvXaN1NrXgefVas5CEqq51WvUohlweUr9Iy3ksOXem4YSEFyeN_RIOYZJDBPnd0I3MxkaFPbTYVUqILD-SuAKFxYFBhFOMxbT</reference>
<id>356175a156a9604b0ed46d21a809d9fa798a744c</id>
<term>
<value>Delhi Cantonment</value>
<offset>0</offset>
</term>
<term>
<value>New Delhi</value>
<offset>18</offset>
</term>
<term>
<value>Delhi</value>
<offset>29</offset>
</term>
<term>
<value>India</value>
<offset>36</offset>
</term>
<matched_substring>
<offset>0</offset>
<length>4</length>
</matched_substring>
</prediction>
<prediction>
<description>Delhi, ON, Canada</description>
<type>locality</type>
<type>political</type>
<type>geocode</type>
<reference>CjQpAAAAfDmekZTvFnEEpCFLn1-hpWDij8X-bYeNzs8yA7NOnSJJ3yapT_OFPDLGi-VXSivKEhDtZcoOZp7JdYzA6JYIRll1GhQb2XZt9on9VDS7mnfBPA-8yjoYlQ</reference>
<id>efdea5b4936bf049207428306448db6fabccaebf</id>
<term>
<value>Delhi</value>
<offset>0</offset>
</term>
<term>
<value>ON</value>
<offset>7</offset>
</term>
<term>
<value>Canada</value>
<offset>11</offset>
</term>
<matched_substring>
<offset>0</offset>
<length>4</length>
</matched_substring>
</prediction>
<prediction>
<description>Delhi, CA, United States</description>
<type>locality</type>
<type>political</type>
<type>geocode</type>
<reference>CjQwAAAAzQLQe_g2_KUMEyM5kJMhOm_hdnk6qx4Djnt_yA1MbWPRQeuT-Ut9XHCOmK1MTFxuEhDqeOLsbn-TpEyAk6vGScGFGhTa6OPk0PGDfMElvpstHYqwngJ0UQ</reference>
<id>bda0d7fb7c9c504032d72df29c79b08dba107d70</id>
<term>
<value>Delhi</value>
<offset>0</offset>
</term>
<term>
<value>CA</value>
<offset>7</offset>
</term>
<term>
<value>United States</value>
<offset>11</offset>
</term>
<matched_substring>
<offset>0</offset>
<length>4</length>
</matched_substring>
</prediction>
<prediction>
<description>Delhi, NY, United States</description>
<type>locality</type>
<type>political</type>
<type>geocode</type>
<reference>CjQwAAAAq1EUig-oKcgx9zYXX9Fe3r3FYa77MlpaWFWg0gqSJy_vSgiki1Uw6s7UhTaXH1KYEhAabBkchKrn6lnpw4Wl21VHGhT9STCizr1QErgYEheGT0Dl5q2GZA</reference>
<id>dd0ac43b6b0b05c55302ed222b8cb4d0c19d1abf</id>
<term>
<value>Delhi</value>
<offset>0</offset>
</term>
<term>
<value>NY</value>
<offset>7</offset>
</term>
<term>
<value>United States</value>
<offset>11</offset>
</term>
<matched_substring>
<offset>0</offset>
<length>4</length>
</matched_substring>
</prediction>
</AutocompletionResponse>
Toast t 仅AutocompletionResponse
显示status
。因此,这意味着只有这两个标签正在被解析。这就是问题所在,所有标签都应该特别解析prediction
和description
.