03-10-2015 08:34 PM - edited 03-14-2019 02:32 PM
Hi
I'd like to retrieve the number of XML nodes in a document to make a script more efficient.
e.g. with this XML, I'd like to know how many <message> nodes there are:
<messages>
<message>contentA</message>
<message>contentA</message>
<message>contentA</message>
</messages>
I've tried iNodeCount = Get XML Document Data (inputXMLfile, "count(//messages/message)")
If I try this in an XPath expression tester then I get the result I'm expecting - an integer of 3. However, in UCCX this produces the error "Can not convert #NUMBER to a NodeList!"... What am I doing wrong?
Thanks
Solved! Go to Solution.
03-10-2015 09:50 PM
Hi,
nothing. The Get XML Document Data step expects the XPath expression return a NodeList.
Apparently, there's some post processing afterwards.
It just gets stuck because this post processing cannot be performed since the XPath expression gave it a single number.
Alternatives:
- use some Java code to do the counting,
- recursively count the matching nodes.
G.
03-10-2015 09:50 PM
Hi,
nothing. The Get XML Document Data step expects the XPath expression return a NodeList.
Apparently, there's some post processing afterwards.
It just gets stuck because this post processing cannot be performed since the XPath expression gave it a single number.
Alternatives:
- use some Java code to do the counting,
- recursively count the matching nodes.
G.
03-10-2015 10:51 PM
Thanks Gergely, I thought that may be the case. If this is what's happening, how can I work around with Java? (I'm currently doing the recursive counting and it's messy plus is problematic because of the 1,000+ executed steps). My first thought was that NodeList could be assigned to an array and a count operation performed on that. However, when I changed iNodeCount to a one-dimensional array it failed script validation... so I'm stumped!
03-11-2015 12:34 AM
Hi,
you may want to use a Set or a Do step to execute Java code.
First, reference the XML file (within the repository) by calling DOC[myFile.xml] and of course, assigning this value to a Document type variable, e.g.:
Set myDocument = DOC[myFile.xml]
Then add a Do step that actually does the XPath part of the job.
There's a nice step by step explanation of it here: http://viralpatel.net/blogs/java-xml-xpath-tutorial-parse-xml/
Remember, you can get the Inputstream object of the myDocument variable using the getInputStream() method on it.
G.
05-26-2015 10:47 PM
Java is the way forward. Recursive looping puts you at risk of exceeding the maximum executed steps. Here's what I did (to save you the pain!)
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(XMLDocument);
javax.xml.xpath.XPath XPath = javax.xml.xpath.XPathFactory.newInstance().newXPath();
String XpathEval = "count(//messages/message)";
Integer RETURNVAL = XPath.evaluate(XpathEval,domDoc);
return RETURNVAL;
}
catch (java.lang.Exception e) {
e.printStackTrace();
}
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