【问题标题】:Parsing XML using java DOM使用 java DOM 解析 XML
【发布时间】:2012-11-18 10:50:18
【问题描述】:

我是 Java 新手,我的任务是使用 http 和当前 url http://belbooner.site40.net/testXmls/details.xml 解析一个 xml 文件

我创建了一些类来使用 Dom 方法解析它,但是我在尝试获取一个 Nodes 值时遇到了 java.lang.NullPointerException 这是代码

import java.security.KeyStore.Builder;
import java.util.*;
import java.io.*;
import java.net.*;
import javax.swing.text.Document;
import javax.xml.*;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.w3c.dom.*;
import org.w3c.dom.CharacterData;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;


public class RequestResponse {
    public static void main(String[] args) throws ParserConfigurationException, IOException, SAXException {
        URL url = new URL("http://belbooner.site40.net/testXmls/details.xml");
        RequestResponse req= new RequestResponse();
        req.getHTTPXml(url);
    }

     void getHTTPXml(URL url) throws ParserConfigurationException, IOException, SAXException {

                //URL url = new URL("http://belbooner.site40.net/testXmls/details.xml");
                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                conn.setRequestMethod("POST");
                conn.setRequestProperty("ACCEPT","application/xml");
                InputStream xml = conn.getInputStream();


                DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
                DocumentBuilder builder = factory.newDocumentBuilder();
                org.w3c.dom.Document document = builder.parse(xml);

                System.out.println(document);
                String doctype = conn.getContentType(); 
                System.out.print(doctype);

                NodeList root = document.getChildNodes();

                Node server = getNodes("server",root);
                Node check = getNodes("check", server.getChildNodes());
                NodeList nodes = check.getChildNodes();

                String checkid= getNodeValue("checkid", nodes);
                System.out.println(checkid);


                conn.disconnect();  

                //return (Document) DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(xml);


     }


    Node getNodes(String tagName, NodeList nodes) {
        for(int i=0; i< nodes.getLength();i++) {
            Node node= nodes.item(i);
            if(node.getNodeName().equalsIgnoreCase(tagName)) {
                return node;
            }

        }
        return null;
    }

    String getNodeValue(String tagName, NodeList nodes ) {
        for ( int i = 0; i < nodes.getLength(); i++ ) {
            Node node = nodes.item(i);
            if (node.getNodeName().equalsIgnoreCase(tagName)) {
                NodeList childNodes = node.getChildNodes();
                for (int y = 0; y < childNodes.getLength(); y++ ) {
                     Node data = childNodes.item(y);
                     if ( data.getNodeType() == Node.TEXT_NODE ) {
                         return data.getNodeValue();
                     }
                     if(data instanceof CharacterData) {
                         CharacterData cd= (CharacterData) data;
                         return cd.getData();
                     }
                 }

            }
        }
        return "";
    }








    }

我得到的堆栈跟踪如下:

application/xmlException in thread "main" java.lang.NullPointerException at 
RequestResponse.getHTTPXml(RequestResponse.java:45) at
RequestResponse.main(RequestResponse.java:22)

Node server = getNodes("server",root); 更改为`

    Node resultNode = getNodes("result", root);
    Node server = getNodes("server", resultNode.getChildNodes());`

   `application/xmlException in thread "main" java.lang.NullPointerException
        at RequestResponse.getHTTPXml(RequestResponse.java:49)
        at RequestResponse.main(RequestResponse.java:22)

`

请帮我找出问题所在。

【问题讨论】:

  • 如果您可以在问题中包含 NullPointerException 的堆栈跟踪,将会很有帮助。
  • 你能分享一下你得到空指针的那一行
  • application/xmlException in thread "main" java.lang.NullPointerException at RequestResponse.getHTTPXml(RequestResponse.java:45) at RequestResponse.main(RequestResponse.java:22)
  • 请打印错误堆栈跟踪
  • @Arshak92 :我已经为您编辑了帖子,以便将堆栈跟踪添加到问题中并回答了您。

标签: java xml dom


【解决方案1】:

问题是 Node server = getNodes("server",root); 返回 null。

为什么会这样?看看你是如何实现 getNodes 的

Node getNodes(String tagName, NodeList nodes) {
    for(int i=0; i< nodes.getLength();i++) {
        Node node= nodes.item(i);
        if(node.getNodeName().equalsIgnoreCase(tagName)) {
            return node;
        }
    }
    return null;
}

您将作为单个“结果”节点的文档根作为输入,您遍历它并比较节点的名称在这种情况下是否为“服务器”,这永远不会,因此您返回 null 并获得一个NPE。

您的节点查找必须通过以下方式完成:

 NodeList root = document.getChildNodes();
// Keep in mind that you have the following structure:
// result
//   server
//    checks
//     check
//      checkId
//     check
//      checkId

Node resultNode = getNodes("result", root);
Node server = getNodes("server", resultNode.getChildNodes());
Node checks = getNodes("checks", server.getChildNodes());
NodeList childNodes = checks.getChildNodes();
for (int i = 0; i < childNodes.getLength(); i++) {
    Node possibleCheck = childNodes.item(i);
    if (possibleCheck.getNodeName().equals("check")) {
    String checkid = getNodeValue("checkid", possibleCheck.getChildNodes());
    System.out.println(checkid);
    }
}

这样您将遍历正确的节点列表。

【讨论】:

  • 我无法更改为root.getChildNodes() as its not a proper Node, maybe you wanted to say NodeList 如您所见,还有另一个名为 root 的 NodeList,我无法从 NodeList 中获取 NodeList
  • @Arshak92:你说得对,对不起,我看到 root 有一个“节点”。我已经编辑了我的答案
  • 谢谢,但我有另一个例外application/xmlException in thread "main" java.lang.NullPointerException at RequestResponse.getHTTPXml(RequestResponse.java:48) at RequestResponse.main(RequestResponse.java:22)
  • 我认为问题出在getNodes方法导致这种异常,我会重点关注它
  • @Arshak92:重新编辑您的问题以添加更新的代码以及更新的异常,以便我或任何其他 stackoverflow 成员可以帮助您。在我完全帮助你之前,你也应该不接受我的回答。
【解决方案2】:

在解析 xml 时使用 XPath 更高效、更灵活(比正常迭代)。

XPath Tutorial from IBM
XPath Reference Orielly tutorial
Xpath Reference Oracle java tutorial

试试下面的代码。

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class RequestResponse {
    public static void main(String[] args) throws ParserConfigurationException,
            IOException, SAXException {
        URL url = new URL("http://belbooner.site40.net/testXmls/details.xml");
        RequestResponse req = new RequestResponse();
        req.getHTTPXml(url);
    }

    void getHTTPXml(URL url) throws ParserConfigurationException, IOException,
            SAXException {
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("POST");
        conn.setRequestProperty("ACCEPT", "application/xml");
        InputStream xml = conn.getInputStream();

        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        org.w3c.dom.Document document = builder.parse(xml);

        System.out.println(document);
        String doctype = conn.getContentType();
        System.out.println(doctype);

        XPathFactory pathFactory = XPathFactory.newInstance();
        XPath path = pathFactory.newXPath();
        XPathExpression expression;
        try {
            expression = path.compile("/result/server/checks/check/checkid");
            NodeList nodeList = (NodeList) expression.evaluate(document,
                    XPathConstants.NODESET);

            String checkids[] = getNodeValue(nodeList);
            for (String checkid : checkids) {
                System.out.print(checkid + ", ");
            }

        } catch (XPathExpressionException e) {
            e.printStackTrace();
        }
        conn.disconnect();

    }

    String[] getNodeValue(NodeList nodes) {
        String checkIds[] = new String[nodes.getLength()];
        for (int i = 0; i < nodes.getLength(); i++) {
            Node node = nodes.item(i);
            checkIds[i] = node.getTextContent();
        }
        return checkIds;
    }

} 

【讨论】:

    猜你喜欢
    • 2012-01-10
    • 2013-01-22
    • 2013-12-24
    • 2011-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-15
    • 2012-09-11
    相关资源
    最近更新 更多