XML文件中獲取來自
我不知道如何通過解析文件並獲得Android的
所需的輸出請幫助..
我的代碼來獲取XML文件:
private void fetchData() {
StringBuilder urlString = new StringBuilder();
urlString
.append("http://maps.google.com/maps/api/directions/xml?origin=");
urlString.append(lat1);
urlString.append(",");
urlString.append(lon1);
urlString.append("&destination=");// to
urlString.append(lat2);
urlString.append(",");
urlString.append(lon2);
urlString.append("&sensor=true&mode=driving");
Log.d("url", "::" + urlString.toString());
HttpURLConnection urlConnection = null;
URL url = null;
try {
url = new URL(urlString.toString());
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setDoOutput(true);
urlConnection.setDoInput(true);
urlConnection.connect();
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
doc = (Document) db.parse(urlConnection.getInputStream());// Util.XMLfromString(response);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
我需要解析文檔對象'doc'。由谷歌返回 示例文件:
<DirectionsResponse>
<status>OK</status>
<route>
<summary>I-40 W</summary>
<leg>
<step>
<travel_mode>DRIVING</travel_mode>
<start_location>
<lat>41.8507300</lat>
<lng>-87.6512600</lng>
</start_location>
<end_location>
<lat>41.8525800</lat>
<lng>-87.6514100</lng>
</end_location>
<polyline>
<points>[email protected]</points>
</polyline>
<duration>
<value>19</value>
<text>1 min</text>
</duration>
<html_instructions>Head <b>north</b> on <b>S Morgan St</b> toward <b>W Cermak Rd</b></html_instructions>
<distance>
<value>207</value>
<text>0.1 mi</text>
</distance>
</step>
...
... additional steps of this leg
...
... additional legs of this route
<duration>
<value>74384</value>
<text>20 hours 40 mins</text>
</duration>
<distance>
<value>2137146</value>
<text>1,328 mi</text>
</distance>
<start_location>
<lat>35.4675602</lat>
<lng>-97.5164276</lng>
</start_location>
<end_location>
<lat>34.0522342</lat>
<lng>-118.2436849</lng>
</end_location>
<start_address>Oklahoma City, OK, USA</start_address>
<end_address>Los Angeles, CA, USA</end_address>
<copyrights>Map data ©2010 Google, Sanborn</copyrights>
<overview_polyline>
<points>[email protected][email protected]`vnApw{A`[email protected]~w\|[email protected]{[email protected]@b}@[email protected][email protected]@jc|Bx}C`[email protected]|@[email protected]}Axf][email protected][email protected]{A~d{A|[email protected]`cFp~xBc`[email protected]@[email protected][email protected]@[email protected]|{CbtY~jGqeMb{iF|n\~mbDzeVh_Wr|Efc\x`Ij{kE}mAb~uF{cNd}xBjp][email protected]|[email protected]_Kv~eGyqTj_|@`uV`k|[email protected]}[email protected][email protected]`CnvHx`[email protected]@j|[email protected]|[email protected]`[email protected][email protected]}[email protected]`@|}[email protected]@jakEitAn{fB_a]lexClshBtmqAdmY_hLxiZd~XtaBndgC</points>
</overview_polyline>
<optimized_waypoint_index>0</optimized_waypoint_index>
<optimized_waypoint_index>1</optimized_waypoint_index>
<bounds>
<southwest>
<lat>34.0523600</lat>
<lng>-118.2435600</lng>
</southwest>
<northeast>
<lat>41.8781100</lat>
<lng>-87.6297900</lng>
</northeast>
</bounds>
</route>
</DirectionsResponse>
什麼是你需要的輸出? –
我需要HTML_instructions標籤裏面的內容出現在數組列表中的每一步 – poo0111