我是node.js新手,需要解析XML。在js中,我只使用DOMParser和getElementsByTagName来获得我想要的值。我刚刚切换到node,正在使用xml2js (愿意考虑其他选择)。我还没有弄清楚如何解析响应来获得我正在寻找的东西。
代码如下:
function parseBody(body){
var parseString = require('xml2js').parseString;
var xml = body
parseString(xml, function (err, result) {
console.dir(result);
});下面是我传递给函数的XML:
<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:int=\"realURLHere">
<soapenv:Header/>
<soapenv:Body>
<int:readResponse>
<response>
<request>?</request>
<cust>
<fName>?</fName>
</cust>
</response>
</int:readResponse>
</soapenv:Body>
</soapenv:Envelope>下面是我的函数的输出:
{ 'soapenv:Envelope':
{ '$':
{ 'xmlns:soapenv': 'http://schemas.xmlsoap.org/soap/envelope/',
'xmlns:int': 'realURLHere' },
'soapenv:Body': [ [Object] ]
}
}我试图在<cust>中访问<fName>的值,但没有成功。任何帮助都将不胜感激。谢谢!
发布于 2019-10-11 05:13:55
如果您将xplicitArray选项设置为false,那么当它解析时,它将不会在您不期望的地方创建那些数组。如下所示:
function parseBody(body) {
var parseString = require('xml2js').parseString;
const options = {
explicitArray: false
};
var xml = body
parseString(xml, options, function (err, result) {
console.log("cust fname: " + result['soapenv:Envelope']['soapenv:Body']['int:readResponse']['response']['cust']['fName']);
})
}
var input = '<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:int=\"realURLHere"><soapenv:Header/><soapenv:Body><int:readResponse><response><request>?</request><cust><fName>?</fName></cust></response></int:readResponse></soapenv:Body></soapenv:Envelope>';
parseBody(input);此外,如果去掉前缀,它会变得更加整洁:
function parseBody(body) {
var parseString = require('xml2js').parseString;
var stripNS = require('xml2js').processors.stripPrefix;
const options = {
tagNameProcessors: [stripNS],
explicitArray: false
};
var xml = body
parseString(xml, options, function (err, result) {
console.log("cust fname: " + result.Envelope.Body.readResponse.response.cust.fName);
})
}
var input = '<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:int=\"realURLHere"><soapenv:Header/><soapenv:Body><int:readResponse><response><request>?</request><cust><fName>?</fName></cust></response></int:readResponse></soapenv:Body></soapenv:Envelope>';
parseBody(input);```发布于 2017-08-10 01:32:49
var custFName = result['soapenv:Envelope']['soapenv:Body'][0]['int:realURLHere'][0]['response'][0]['cust'][0]['fName'];
这解决了我的问题。我的努力与这个响应有关,所有的东西都在一个数组中。如果有更简单的方法或更动态的方式,请让我知道。但就目前而言,这是可行的。
发布于 2017-08-10 18:01:36
尝尝这个
const transform = require('camaro')
const xml = `
<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:int=\"realURLHere">
<soapenv:Header/>
<soapenv:Body>
<int:readResponse>
<response>
<request>?</request>
<cust>
<fName>hello world</fName>
</cust>
</response>
</int:readResponse>
</soapenv:Body>
</soapenv:Envelope>
`
const template = {
fname: '//cust/fName'
}
const result = transform(xml, template)
console.log(JSON.stringify(result, null, 2))哪种打印输出
{
"fname": "hello world"
}您可以使用result.fname进行访问
https://stackoverflow.com/questions/45594547
复制相似问题