September 24, 200916 yr I haven't looked closely at the XML libraries with SM, but, I didn't see any mention of Xpath. Is there a way to use an existing jar file with SM for Xpath support? Anyone gone this route yet with pointers on exactly what to do?
October 22, 200916 yr Looks like XPath functionality is included with Java, since Java 1.5 http://java.sun.com/j2se/1.5.0/docs/api/javax/xml/xpath/XPath.html Please let us know if you're interested in us estimating a custom ScriptMaster module for you.
February 16, 201015 yr I created this with the help of some folks at StackOverflow: RegisterGroovy( "XPath( xml ; query )" ; "// XPath (xml, query)¶ // Created by Jack James with help from StackOverflow.com¶ // Version 1 16/02/10¶ ¶ import javax.xml.xpath.*¶ import javax.xml.parsers.DocumentBuilderFactory¶ ¶ def processXml( String xml, String xpathQuery ) {¶ def xpath = XPathFactory.newInstance().newXPath()¶ def builder = DocumentBuilderFactory.newInstance().newDocumentBuilder()¶ def inputStream = new ByteArrayInputStream( xml.bytes )¶ def records = builder.parse(inputStream).documentElement¶ def nodes = xpath.evaluate( xpathQuery, records, XPathConstants.NODESET )¶ nodes.collect { node -> node.textContent }¶ ¶ }¶ ¶ processXml( xml, query )" ) Put that in a step variable and call it with the xml to process as the first parameter and the XPath query as the second. It's fairly basic, so if anyone wants to expand on it, be my guest (I know nothing of java/groovy unfortunately)
June 16, 201015 yr Very simple xml query for a simple fm_fileIn contains fm_query = any valid node returns the node value or '0' if empty or not exist RegisterGroovy( "XmlSlurp( fm_fileIn ; fm_query )" ; "// XmlSlurp(fm_fileIn ; fm_query )¶ // 15_06_10 JR¶ // v1¶ ¶ xs = new XmlSlurper().parseText(fm_fileIn)¶ ln = xs."${fm_query}".size()¶ if ( ln == 1 ) {¶ return xs."${fm_query}".text().trim()¶ } else return false¶ ¶ // finds length of trimmed node¶ //xs."${fm_query}".text().trim().toString().length()" )
September 9, 201015 yr Please note that the current version of the XPath function (as distributed with SM4) will break if you attempt to input xml with UTF-8 encoded characters and no header. You can force it to always accept UTF-8 by making the following change: InputStream inputStream = new ByteArrayInputStream( someXML.getBytes("utf-8"));
Create an account or sign in to comment