尝试从Document中检索Java中的节点值时出错

时间:2014-05-07 19:20:43

标签: java xml rest nodes domdocument

我有一个程序,它接收URL并从URL构造一个Document文件,然后传递给printResponse方法。

以下是printResponse代码:

  private static void printResponse(Document doc) throws TransformerException, FileNotFoundException {
        Transformer trans = TransformerFactory.newInstance().newTransformer();
        Properties props = new Properties();
        props.put(OutputKeys.INDENT, "yes");
        trans.setOutputProperties(props);
        StreamResult res = new StreamResult(new StringWriter());
       // String sr1 = doc.getAttributes().getNamedItem("Amount").getNodeValue();
       // System.out.println(sr1);
        DOMSource src = new DOMSource(doc);
        trans.transform(src, res);

        String toString = res.getWriter().toString();

        System.out.println(toString);
    }

当我运行这样的代码时,它工作正常,花花公子并打印出XML文件的整个结构。

但是,如果我运行这样的代码:

    private static void printResponse(Document doc) throws TransformerException, FileNotFoundException {
    Transformer trans = TransformerFactory.newInstance().newTransformer();
    Properties props = new Properties();
    props.put(OutputKeys.INDENT, "yes");
    trans.setOutputProperties(props);
    StreamResult res = new StreamResult(new StringWriter());
    String sr1 = doc.getAttributes().getNamedItem("Amount").getNodeValue();
    System.out.println(sr1);
    DOMSource src = new DOMSource(doc);
    trans.transform(src, res);

   // String toString = res.getWriter().toString();

   // System.out.println(toString);
}

现在;我知道我有一个名为“500”的节点,它打印出第一个设置,那么为什么我无法使用Java API尝试的代码访问该500值?

第二组代码的错误输出:

java.lang.NullPointerException
    at sample.ItemLookupSample.printResponse(ItemLookupSample.java:218)
    at sample.ItemLookupSample.main(ItemLookupSample.java:200)

第218行是:

String sr1 = doc.getAttributes().getNamedItem("Amount").getNodeValue();

和第200行是:

printResponse(response);

编辑:

尝试循环通过节点:

Node n = doc.getFirstChild();
    NodeList mnl = n.getChildNodes();
    Node an, an1;

    for (int i = 0; i < mnl.getLength(); i++) {
        an = mnl.item(i);
        if (an.getNodeType() == Node.ELEMENT_NODE) {
            NodeList nl2 = an.getChildNodes();
            for (int z = 0; z < nl2.getLength(); z++) {
                an1 = nl2.item(z);

                if (an1.hasChildNodes()) {
                    System.out.println(an1.getFirstChild().getTextContent());
                    System.out.println(an1.getFirstChild().getNodeValue());
                }
                System.out.println(an1.getTextContent());
                System.out.println(an1.getNodeValue());
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }

返回值:

null

null
fb6c84a9-9d63-4a2d-beed-d5795d6927e5
fb6c84a9-9d63-4a2d-beed-d5795d6927e5
fb6c84a9-9d63-4a2d-beed-d5795d6927e5
null

null

null
0.0856150000000000
0.0856150000000000
0.0856150000000000
null
True
null
TrueASIN1451648537LargeAll
null
1451648537
null

HUGE WALL OF TEXT HERE! (The description of a book from the URL page)

null

编辑3:

添加一个三级循环以深入嵌套打印出我正在寻找的值,但也有一堆其他值;我将尝试优化解决方案,然后在此处发布。这是我的循环现在的样子。

Node n = doc.getFirstChild();
    NodeList mnl = n.getChildNodes();
    Node an1, an2, an3;

    for (int i = 0; i < mnl.getLength(); i++) {
        an1 = mnl.item(i);
        if (an1.getNodeType() == Node.ELEMENT_NODE) {
            NodeList nl2 = an1.getChildNodes();
            for (int z = 0; z < nl2.getLength(); z++) {
                an2 = nl2.item(z);
                if(an2.getNodeType() == Node.ELEMENT_NODE){
                    NodeList nl3 = an2.getChildNodes();
                    for (int y = 0; y < nl3.getLength(); y++){
                        an3 = nl3.item(y);
                        if (an3.hasChildNodes()) {
                            System.out.println(an3.getFirstChild().getTextContent());
                            System.out.println(an3.getFirstChild().getNodeValue());
                        }
                        System.out.println(an3.getLocalName());
                        System.out.println(an3.getTextContent());
                        System.out.println(an3.getNodeValue());
                        System.out.println("");
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
    }

1 个答案:

答案 0 :(得分:1)

阅读javadocs:  的getAttributes

NamedNodeMap getAttributes()

A NamedNodeMap containing the attributes of this node (if it is an *Element*) or null otherwise. 

Document接口表示整个HTML或XML文档。从概念上讲,它是文档树的根。

null.getNamedItem(&#34; Amount&#34;)???

也许打破分开的步骤?