簡體   English   中英

Android簡單XML解析

[英]Android Simple XML Parsing

我使用以下網站查找海拔:

http://gisdata.usgs.gov/xmlwebservices2/elevation_service.asmx/getElevation?X_Value=-78.85834070853889&Y_Value=43.869369104504585&Elevation_Units=METERS&Source_Layer=-1&Elevation_Only=true

這以XML格式提供格式為93.7665481567383的結果。

有人知道為我的android程序提取這些數據的快速而簡單的方法嗎?

我嘗試了以下但是我一直得到“null”作為輸出。

HttpURLConnection connection = null;
URL serverAddress = null;
serverAddress = new URL("http://gisdata.usgs.gov/xmlwebservices2/elevation_service.asmx/getElevation?X_Value=-78.85834070853889&Y_Value=43.869369104504585&Elevation_Units=METERS&Source_Layer=-1&Elevation_Only=true");

connection = null;
connection = (HttpURLConnection)serverAddress.openConnection();
connection.setRequestMethod("GET");
connection.setDoOutput(true);
connection.setReadTimeout(10000);

connection.connect();
BufferedReader rd  = new BufferedReader(new
InputStreamReader(connection.getInputStream()));
StringBuilder sb = new StringBuilder();

String line = null;
line = rd.readLine();
while ((line = rd.readLine()) != null)
{
    sb.append(line + '\n');
}

當我輸出sb.toString()時,我得到Null

有任何想法嗎?

你犯了一個小錯誤:

第一個rd.readLine()返回你的<tag>number</tag>但是while循環再次用null擦除它。 刪除第一個rd.readLine()調用,它應該工作。

String line = null;
line = rd.readLine(); // you get the only line... remove it!
while ((line = rd.readLine()) != null) {
    sb.append(line + '\n');
}

要獲得您的號碼,請嘗試使用以下內容:

// line should contain your string...
line = line.substring(line.indexOf(">"), line.indexOf("</"));

在沒有實際嘗試這個的情況下,我建議嘗試解析 XML而不是嘗試從Buffer中讀取它。 要非常詳細地執行此操作,您需要知道XML樹結構,因為您將基於Nodes讀取。 例如:

// Data members
private URL URL;
private InputStream stream;
private DocumentBuilder builder;
private Document document;
private Element root;
private NodeList nodeList;

URL = new URL(url); // The URL of the site you posted goes here.
stream = URL.openStream();

// Set up and initialize the document.
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setIgnoringElementContentWhitespace(true);
builder = dbf.newDocumentBuilder();
document = builder.parse(stream);
document.getDocumentElement().normalize();

root = document.getDocumentElement();
nodeList = root.getChildNodes();


// The number of calls to 'getFirstChild()' will vary with the complexity of
// your XML tree. Also, 'i' could vary as well, depending on which Node off
// of the root you want to access.
String number = nodeList.item(i).getFirstChild().getFirstChild().getNodeValue();

這可能看起來很混亂,但讓我舉一個XML的例子。

<Building>
    <name>Train Station</name>
    <address>8 Main Street</address>
    <color>Blue</color>
</Building>
<Building>
    <name>Drugstore</name>
    <address>14 Howard Boulevard</address>
    <color>Yellow</color>
</Building>

每個建築物將表示作為參數傳遞給'.item(i)'的不同值。 要訪問有關第一個Building的信息,請傳遞值0.使用'.getFirstChild()'方法訪問Building的子節點,但這只返回Node。 如果你想要文本“葯店”,你必須遍歷XML樹到第二項的第一個孩子的數據,例如。 nodeList.item(1).getFirstChild().getNodeValue();

我希望有幫助!!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM