简体   繁体   English

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

[英]Error when attempting to retrieve node value in Java from Document

I have a program that takes in a URL and from that URL constructs a Document file that is then passed to a printResponse method. 我有一个接受URL的程序,并从该URL构造一个Document文件,然后将其传递给printResponse方法。

Here is the printResponse code: 这是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);
    }

when I run the code like this it works fine and dandy and prints out the entire structure of the XML file. 当我像这样运行代码时,它工作得很好且花哨,并打印出XML文件的整个结构。

However if I run the code like this: 但是,如果我运行这样的代码:

    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);
}

Now; 现在; I know I have a node named "500" that prints out with the first set up, so why am I unable to access that 500 value with the code I attempted using the Java API? 我知道我有一个名为“ 500”的节点,该节点会在第一次设置时打印出来,那么为什么我无法使用尝试使用Java API的代码访问该500值?

Error output for second set of code: 第二组代码的错误输出:

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

Where line 218 is: 第218行是:

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

and line 200 is: 第200行是:

printResponse(response);

Edit: 编辑:

Attempt at a loop through the nodes: 尝试遍历节点:

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();
                }
            }
        }

Returned values: 返回值:

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

Edit 3: 编辑3:

Adding a tertiary loop to go deeper into the nesting printed out the value I was looking for but a bunch of other values as well; 添加一个第三级循环以更深入地嵌套,可以打印出我一直在寻找的值,但也列出了其他一些值; I will attempt to refine the solution then post it here. 我将尝试优化解决方案,然后将其发布在此处。 Here is what my loop looks like as of now. 这是我的循环到目前为止的样子。

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();
                        }
                    }
                }
            }
        }
    }

Just reading the javadocs: getAttributes 只需阅读javadocs:getAttributes

NamedNodeMap getAttributes() NamedNodeMap getAttributes()

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

The Document interface represents the entire HTML or XML document. Document接口代表整个HTML或XML文档。 Conceptually, it is the root of the document tree. 从概念上讲,它是文档树的根。

null.getNamedItem("Amount") ??? null.getNamedItem(“ Amount”)???

Maybe break the steps apart? 也许将步骤分开?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android Azure:404尝试从服务器检索数据时出错 - Android Azure: 404 Error when attempting to retrieve data from the Server Java错误尝试将方法的返回值用作if语句中的项时 - Java Error When attempting to use the return from a method as an item in an if statement 尝试显示菜单并从用户检索输入时Java程序崩溃(NoSuchElementException) - Java program crashing when attempting to show a menu and retrieve input from user (NoSuchElementException) 尝试从手机获取KML节点值时获取null值,但在Android模拟器上无法获取 - Getting a null value when attempting to get KML node value from phone, but not on the emulator with android 尝试构建XML文档Java时获取DOMException - Getting a DOMException when attempting to build an XML document Java 尝试在Java中使用自定义比较器时出错 - Error when attempting to use custom comparator in Java 当我的XML标记中包含“ _”时,无法从文档中检索节点列表 - Not able to retrieve Node List from Document when my XML tag contains “_” in it 尝试从输入流中检索文本时,应用程序挂起 - Application hangs when attempting to retrieve text from an inputstream 尝试检索 AuthnRequest 时出现 ZipException - ZipException when Attempting to retrieve AuthnRequest 使用Java从xslt文档中检索所有xsl:element和xsl:attribute标记的name属性的值 - retrieve value of name attribute of all xsl:element and xsl:attribute tag from xslt document Using java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM