Welcome Guest! Log in
×

Notice

The forum is in read only mode.
Stambia versions 2.x, 3.x, S17, S18, S19 and S20 are reaching End of Support January, 15th, 2024. Please consider upgrading to the supported Semarchy xDI versions. See Global Policy Support and the Semarchy Documentation.

The Stambia User Community is moving to Semarchy! All the applicable resources have already been moved or are currently being moved to their new location. Read more…

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
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.