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.
Here is the printResponse
code:
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.
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?
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:
String sr1 = doc.getAttributes().getNamedItem("Amount").getNodeValue();
and line 200 is:
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:
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();
}
}
}
}
}
}