Bill Doerrfeld Posted September 24, 2009 Posted September 24, 2009 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?
Valentin Posted October 22, 2009 Posted October 22, 2009 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.
wintergreen Posted February 16, 2010 Posted February 16, 2010 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)
Valentin Posted February 17, 2010 Posted February 17, 2010 Thanks! If you don't mind we'll add this to the next release of ScriptMaster.
john renfrew Posted June 16, 2010 Posted June 16, 2010 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()" )
wintergreen Posted September 9, 2010 Posted September 9, 2010 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"));
Valentin Posted September 10, 2010 Posted September 10, 2010 Thank you, we'll add this to the next release.
Recommended Posts
This topic is 5199 days old. Please don't post here. Open a new topic instead.
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now