Welcome Guest! Log in

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.

Please Log in or Create an account to join the conversation.