Welcome Guest! Log in
×

Notice

The forum is in read only mode.
Due to some maintenance operations, stambia.org will be switched to read-only mode during the 13th November. It will be possible to read and download, but impossible to post on the forums or create new accounts. For any question please contact the support team.

Topic-icon Question How to query an XML file using XPath in a Scripting action ?

  • Nicolas Verscheure
  • Nicolas Verscheure's Avatar Topic Author
  • Offline
More
22 Nov 2014 18:01 - 22 Nov 2014 18:04 #1 by Nicolas Verscheure
Nicolas Verscheure created the topic: How to query an XML file using XPath in a Scripting action ?
If you want to query an XML file, at first sight, you must use a mapping. But it's like using a tank to kill a mouse !
Fortunately, we can do better with scripting.
The following example shows how to open an XML file and how to query it with an XPath expression :

factory = javax.xml.parsers.DocumentBuilderFactory.newInstance(); 
factory.setNamespaceAware(true); 
builder = factory.newDocumentBuilder(); 
doc = builder.parse("%x{md:paramValue($this,'XML_FILE_NAME')}x%"); 
xpathFactory = javax.xml.xpath.XPathFactory.newInstance(); 
xpath = xpathFactory.newXPath(); 
expr = xpath.compile("%x{md:paramValue($this,'XPATH_EXPRESSION')}x%"); 
result = expr.evaluate(doc,javax.xml.xpath.XPathConstants.STRING); 
__ctx__.publishVariable("./QUERY_RESULT",result);


Attachments:
Last Edit: 22 Nov 2014 18:04 by Nicolas Verscheure.