【问题标题】:Android Simple XML ParsingAndroid 简单 XML 解析
【发布时间】:2012-01-24 15:34:19
【问题描述】:

我正在使用以下网站查找海拔:

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

有什么想法吗?

【问题讨论】:

  • 您是否在清单中添加了 权限?
  • 是的,我已经将它添加到清单中。还是不行

标签: android xml xml-parsing


【解决方案1】:

你犯了一个小错误:

第一个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("</"));

【讨论】:

  • 好点,谢谢。但是,这并没有解决问题,我能够将问题追溯到 InputStreamReader(connection.getInputStream()));我得到的错误是: D/gps (2908): java.io.FileNotFoundException: gisdata.usgs.gov/xmlwebservices2/elevation_service.asmx/… 89&Y_Value=43.869369104504585&Elevation_Units=METERS&Source_Layer=-1&Elevation_Only=true 有什么想法吗?
【解决方案2】:

如果我自己没有实际尝试,我建议尝试解析 XML,而不是尝试从缓冲区中读取它。要详细执行此操作,您需要了解 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。如果您想要文本“Drugstore”,则必须遍历 XML 树到第二项的第一个子项的数据,例如。 nodeList.item(1).getFirstChild().getNodeValue();

希望对你有所帮助!!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-07
    • 2016-10-13
    • 1970-01-01
    • 1970-01-01
    • 2017-12-07
    相关资源
    最近更新 更多