有人知道如何在ECMA脚本(Datapower)中使用xpath表达式来访问XML数据吗?
IBM infocenter没有关于如何访问XML数据的信息
如果您有任何用于访问XML数据的示例脚本,请提供
谢谢
发布于 2014-12-05 21:33:28
在实现的ECMA (Node.js)中,GatewayScript不支持任何XML。但是,我已经成功地使用了XPATH和DOM模块。下载XMLDom (https://github.com/jindw/xmldom)和Xpath (https://github.com/goto100/xpath) Node.js模块,并将以下脚本添加到您的DP目录:
要在XML中使用它,首先需要从INPUT获取DataPower数据:
// This is where we start, grab the INPUT as a buffer
session.input.readAsBuffers(function(readAsBuffersError, data) {
if (readAsBuffersError) {
console.error('Error on readAsBuffers: ' + readAsBuffersError);
session.reject('Error on readAsBuffers: ' + readAsBuffersError);
} else {
if (data.slice(0,5).toString() === '<?xml') {
console.log('It is XML!');
parseXML(data);
}
} //end read as buffers error
}); //end read as buffer function
function parseXML(xml) {
// Load XML Dom and XPath modules
var select = require('local:///xpath.js');
var dom = require('local:///dom-parser.js');
var doc = new dom.DOMParser().parseFromString(xml.toString(), 'text/xml');
// Get attribute
var nodes = select(doc, "//root/element1/@protocol");
try {
var val = nodes[0].value.toString();
console.log('found xml attribute as ['+val+']');
} catch(e) {
// throw error here
}
// Get an element
nodes = select(doc, "//root/element1/child1");
try {
var val = nodes[0].firstChild.data;
console.log('elemnt found as ['+val+']');
} catch(e) {
//throw error here
}
}这应该是一个工作样本..。如果移动模块,则需要更改模块的路径。我在store:/中有一个目录,我可以在其中添加我的GWS模块。
希望你能让它飞起来!
发布于 2017-06-22 21:38:40
至少从7.0.0固件版本开始,Gatewayscript能够很容易地与XPATH和DOM一起工作。DP存储中的代码片段:
//reading body from the rule input
session.input.readAsXML(function (error, nodeList) {
if (error) {
//error behaviour
} else {
var domTree;
try {
domTree = XML.parse(nodeList);
} catch (error) {
//error behaviour
}
var transform = require('transform'); //native gatewayscript module
transform.xpath('/someNode/anotherNode/text()', domTree, function(error, result){
if(error){
//error behaviour
}
//some use of result, for example putting it to output
session.output.write(result);
}
});
});https://stackoverflow.com/questions/26582051
复制相似问题