I have the following XML code:
<CampaignFrameResponse
xmlns="http://Qsurv/api"
xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<Message>Success</Message>
<Status>Success</Status>
<FrameHeight>308</FrameHeight>
<FrameUrl>http://delivery.usurv.com?Key=a5018c85-222a-4444-a0ca-b85c42f3757d&ReturnUrl=http%3a%2f%2flocalhost%3a8080%2feveningstar%2fhome</FrameUrl>
</CampaignFrameResponse>
What I'm trying to do is extract the nodes and assign them to a variable. So for example, I'd have a variable called FrameHeight containing the value 308.
This is the Java code I have so far:
private void processNode(Node node) {
NodeList nodeList = node.getChildNodes();
for (int i = 0; i < nodeList.getLength(); i++) {
Node currentNode = nodeList.item(i);
if (currentNode.getNodeType() == Node.ELEMENT_NODE) {
//calls this method for all the children which is Element
LOG.warning("current node name: " + currentNode.getNodeName());
LOG.warning("current node type: " + currentNode.getNodeType());
LOG.warning("current node value: " + currentNode.getNodeValue());
processNode(currentNode);
}
}
}
This prints out the node names, types and values, but what is the best way of assigning each of the values to an appropriately-named variable? eg int FrameHeight = 308?
This is my updated code where the nodeValue variable keeps returning null:
processNode(Node node) {
NodeList nodeList = node.getChildNodes();
for (int i = 0; i < nodeList.getLength(); i++) {
Node currentNode = nodeList.item(i);
if (currentNode.getNodeType() == Node.ELEMENT_NODE) {
//calls this method for all the children which is Element
String nodeName = currentNode.getNodeName();
String nodeValue = currentNode.getNodeValue();
if(nodeName.equals("Message")) {
LOG.warning("nodeName: " + nodeName);
message = nodeValue;
LOG.warning("Message: " + message);
}
else if(nodeName.equals("FrameHeight")) {
LOG.warning("nodeName: " + nodeName);
frameHeight = nodeValue;
LOG.warning("frameHeight: " + frameHeight);
}
processNode(currentNode);
}
}
}