[英]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.