【问题标题】:null pointer exception on getNodeValue() when parsing XML web page - android解析 XML 网页时 getNodeValue() 出现空指针异常 - android
【发布时间】:2014-07-15 17:39:50
【问题描述】:

我查看了此处发布的几个答案,但找不到我需要的答案。它可能与网站本身有关,但我认为不是。 我正在尝试解析网站上的 XML,但遇到 空指针异常 错误。

我运行解析是一个单独的线程,在从网络上阅读时遵循 Google 的需求。

请查看我的代码并尝试提供帮助。

class BackgroundTask1 extends AsyncTask<String, Void, String[]> {

protected String[] doInBackground(String... url) {
    new HttpGet();
    new StringBuffer();
    InputStream is = null;
    HttpURLConnection con = null;

    try {
        //Log.d("eyal", "URL: " + boiUrl);
        URL url1 = new URL("http://www.boi.org.il/currency.xml");
        con = (HttpURLConnection)url1.openConnection();
        con.setRequestMethod("GET");
        con.connect();
        is = con.getInputStream();
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document doc = builder.parse(is);
        NodeList lastVld = doc.getElementsByTagName("LAST_UPDATE");
        String lastV = lastVld.item(0).getFirstChild().getNodeValue();
        }

        catch (Exception e) {
           e.printStackTrace();
        }

我在最后一行得到错误。

感谢您的帮助。

【问题讨论】:

  • 您在代码中的哪个位置获得了 NPE?
  • Ryan J,回答你的问题- 在 lastVld.item(0).getFirstChild().getNodeValue();

标签: java android xml parsing xml-parsing


【解决方案1】:

这段代码对我有用

InputStream is = null;
HttpURLConnection con = null;

try {
    URL url1 = new URL("http://www.boi.org.il/currency.xml");
    con = (HttpURLConnection)url1.openConnection();
    con.setRequestMethod("GET");
    con.connect();
    is = con.getInputStream();
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document doc = builder.parse(is);
    NodeList lastVld = doc.getElementsByTagName("LAST_UPDATE");

    Element elem = (Element) lastVld.item(0);
    String lastV = elem.getTextContent();
    System.out.println(lastV);
} catch (Exception e) {
    e.printStackTrace();
}

我通过添加转换器将结果打印到控制台来验证我获得了良好的内容。

TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer xform = tFactory.newTransformer();

xform.transform(new DOMSource(doc), new StreamResult(System.out));

有几次我尝试运行 elem 的结果为 null,我认为这与从 URL 中检索到的一些不良内容有关。这是transformer打印出来的内容。

<html>
<body>
<script>document.cookie='iiiiiii=11a887d6iiiiiii_11a887d6; path=/';window.location.href=window.location.href;</script>
</body>
</html>

我注意到,如果我在浏览器中打开这个文件,代码会突然停止工作,直到我刷新页面,然后它才开始给我正确的输出。

我怀疑这个 URL 有问题,因为当它正常工作时,这段代码工作正常。

祝你好运……

【讨论】:

  • 感谢您的努力。你是对的,网站有问题。它已修复,现在正在运行。我在您的回答中为您添加了 V。
【解决方案2】:

您的 xml 中只有一个 LAST_UPDATE 标记,并且它有一个内部值,因此请尝试仅使用您从 item(0) 获得的 Node Class 中的节点值

 String lastV = lastVld.item(0).getNodeValue();

HTH

【讨论】:

  • 抱歉,还是不行。你能运行这段代码吗?它对你有用吗?
【解决方案3】:

没有为该标签名称返回节点。您可能需要先检查 lastVld 的大小,然后尝试访问其中的项目。

【讨论】:

  • 本站只有一个LAST_UPDATE节点,看不懂你的评论。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-11-04
  • 2023-03-31
  • 2012-02-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-30
相关资源
最近更新 更多