Java 分析XML网页时getNodeValue()上出现空指针异常-android

Java 分析XML网页时getNodeValue()上出现空指针异常-android,java,android,xml,parsing,xml-parsing,Java,Android,Xml,Parsing,Xml Parsing,我看了这里贴的几个答案,但找不到我需要的答案。这可能与网站本身有关,但我不这么认为。 我试图解析网站上的XML,但得到一个空指针异常错误 我运行解析是一个单独的线程,在从web读取时遵循Google的要求 请查看我的代码并尝试提供帮助 class BackgroundTask1 extends AsyncTask<String, Void, String[]> { protected String[] doInBackground(String... url) { new

我看了这里贴的几个答案,但找不到我需要的答案。这可能与网站本身有关,但我不这么认为。 我试图解析网站上的XML,但得到一个空指针异常错误

我运行解析是一个单独的线程,在从web读取时遵循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();
        }
class BackgroundTask1扩展了AsyncTask{
受保护的字符串[]doInBackground(字符串…url){
新的HttpGet();
新的StringBuffer();
InputStream=null;
HttpURLConnection con=null;
试一试{
//Log.d(“eyal”,“URL:+boiUrl”);
URL url1=新URL(“http://www.boi.org.il/currency.xml");
con=(HttpURLConnection)url1.openConnection();
con.setRequestMethod(“GET”);
con.connect();
is=con.getInputStream();
DocumentBuilderFactory工厂=DocumentBuilderFactory.newInstance();
DocumentBuilder=factory.newDocumentBuilder();
文档doc=builder.parse(is);
NodeList lastVld=doc.getElementsByTagName(“上次更新”);
字符串lastV=lastVld.item(0.getFirstChild().getNodeValue();
}
捕获(例外e){
e、 printStackTrace();
}
我在最后一行得到了错误


谢谢您的帮助。

您的xml中只有一个最后更新标记,并且它有一个内部值,因此请尝试使用从
项(0)中获取的节点值。


HTHs

没有为该标记名返回节点。您可能需要首先检查最后一个VLD的大小,然后尝试访问其中的项目。

此代码对我有效

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
结果为空,我认为这与从URL检索到的一些不好的内容有关。这是由transformer打印的内容

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

document.cookie='iiiiiii=11a887d6iiiiiii_11a887d6;path=/';window.location.href=window.location.href;
我注意到如果我在浏览器中打开这个文件,代码会突然停止工作,直到我刷新页面,然后它开始给我正确的输出

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


祝你好运…

你的代码中哪里有NPE?Ryan J,回答你的问题-在lastVld.item(0.getFirstChild().getNodeValue()行中;此网站中只有一个“最后更新”节点,我不理解您的评论。很抱歉,仍然不工作。您能够运行此代码吗?它对您有效吗?谢谢您的努力。您是对的,网站有问题。它已修复,现在正在工作。我在您的回答中为您添加了V。
<html>
<body>
<script>document.cookie='iiiiiii=11a887d6iiiiiii_11a887d6; path=/';window.location.href=window.location.href;</script>
</body>
</html>