Jump to content
Claris Engage 2025 - March 25-26 Austin Texas ×

This topic is 5199 days old. Please don't post here. Open a new topic instead.

Recommended Posts

Posted

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?

  • 4 weeks later...
Posted

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.

  • 3 months later...
Posted

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)

  • 3 months later...
Posted

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()" )

  • 2 months later...
Posted

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"));

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.