02-16-2020 07:36 PM
I am trying to write script for a callback application which will check if the caller has already left a call for a callback recently.
The xml will be something like
<CallBack>
<Filename>CB.xml</Filename>
<CB>
<DATE>07/02/2020</DATE>
<TIME>12:00</TIME>
<NUM>0400000000</NUM>
<Done>True</Done>
</CB>
<CB>
<DATE>08/02/2020</DATE>
<TIME>11:00</TIME>
<NUM>0288888888</NUM>
<Done>True</Done>
</CB>
<CB>
<DATE>09/02/2020</DATE>
<TIME>13:00</TIME>
<NUM>0388888888</NUM>
<Done>True</Done>
</CB>
<CB>
<DATE>12/02/2020</DATE>
<TIME>12:00</TIME>
<NUM>0400000000</NUM>
<Done>False</Done>
</CB>
</CallBack>
I have this set step
try {
javax.xml.parsers.DocumentBuilderFactory factory = javax.xml.parsers.DocumentBuilderFactory.newInstance();
javax.xml.parsers.DocumentBuilder builder = factory.newDocumentBuilder();
org.w3c.dom.Document domDoc = builder.parse(doc_InputFile);
javax.xml.xpath.XPath XPath = javax.xml.xpath.XPathFactory.newInstance().newXPath();
String XpathEval = str_XPATH;
String RESULT = XPath.evaluate("/CallBack/CB[NUM=0400000000]",domDoc);
return RESULT;
}
catch (java.lang.Exception e) {
e.printStackTrace();
}
But it only returns the first node from the xml.
U"\n\t07/02/2020\n 12:00\n\t0400000000\n\tTrue\n "
So I tried to change it to return a nodelist as below
try {
javax.xml.parsers.DocumentBuilderFactory factory = javax.xml.parsers.DocumentBuilderFactory.newInstance();
javax.xml.parsers.DocumentBuilder builder = factory.newDocumentBuilder();
org.w3c.dom.Document domDoc = builder.parse(doc_InputFile);
javax.xml.xpath.XPath XPath = javax.xml.xpath.XPathFactory.newInstance().newXPath();
String XpathEval = str_XPATH;
NodeList nodeList = (NodeList) xPath.compile("/CallBack/CB[NUM=0400000000]").evaluate(domDoc, XPathConstants.NODESET);
for (int i = 0; i < nodeList.getLength(); i++) {
String RESULT = RESULT + (nodeList.item(i).getFirstChild().getNodeValue());
}
return RESULT;
}
catch (java.lang.Exception e) {
e.printStackTrace();
}
I am getting an error on the NodeList line "Unable to parse expression; invalid identifier: NodeList.
It maybe I need to declare these but I am not sure of the syntax required
org.w3c.dom.Node;
org.w3c.dom.NodeList;
Regards,
Paul
02-17-2020 05:13 PM
02-18-2020 10:07 PM
This is the code I used that returns the values for each element that matches a node in the xml.
{
try {
javax.xml.parsers.DocumentBuilderFactory factory = javax.xml.parsers.DocumentBuilderFactory.newInstance();
javax.xml.parsers.DocumentBuilder builder = factory.newDocumentBuilder();
org.w3c.dom.Document domDoc = builder.parse(doc_InputFile);
javax.xml.xpath.XPath XPath = javax.xml.xpath.XPathFactory.newInstance().newXPath();
org.w3c.dom.NodeList nodeList = (org.w3c.dom.NodeList) XPath.compile("/CallBack/CB[NUM=0400000000]").evaluate(domDoc, javax.xml.xpath.XPathConstants.NODESET);
int i = 0;
for (i = 0; i < nodeList.getLength(); i++) {
org.w3c.dom.Node node = nodeList.item(i);
org.w3c.dom.Element eElement = (org.w3c.dom.Element) node;
String Date = eElement.getElementsByTagName("DATE").item(0).getTextContent();
String Time = eElement.getElementsByTagName("TIME").item(0).getTextContent();
String Num = eElement.getElementsByTagName("NUM").item(0).getTextContent();
String Done = eElement.getElementsByTagName("Done").item(0).getTextContent();
RESULT = RESULT + Date + Time + Num + Done;
}
return RESULT;
}
catch (java.lang.Exception e) {
e.printStackTrace();
return null;
}
Discover and save your favorite ideas. Come back to expert answers, step-by-step guides, recent topics, and more.
New here? Get started with these tips. How to use Community New member guide