首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用xml2js进行解析

使用xml2js进行解析
EN

Stack Overflow用户
提问于 2017-08-09 23:13:44
回答 4查看 3.6K关注 0票数 3

我是node.js新手,需要解析XML。在js中,我只使用DOMParsergetElementsByTagName来获得我想要的值。我刚刚切换到node,正在使用xml2js (愿意考虑其他选择)。我还没有弄清楚如何解析响应来获得我正在寻找的东西。

代码如下:

代码语言:javascript
复制
function parseBody(body){
    var parseString = require('xml2js').parseString;
    var xml = body
    parseString(xml, function (err, result) {
    console.dir(result);
});

下面是我传递给函数的XML:

代码语言:javascript
复制
<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>

下面是我的函数的输出:

代码语言:javascript
复制
{ 'soapenv:Envelope':
   { '$':
      { 'xmlns:soapenv': 'http://schemas.xmlsoap.org/soap/envelope/',
        'xmlns:int': 'realURLHere' },
     'soapenv:Body': [ [Object] ] 
   } 
}

我试图在<cust>中访问<fName>的值,但没有成功。任何帮助都将不胜感激。谢谢!

EN

回答 4

Stack Overflow用户

发布于 2019-10-11 05:13:55

如果您将xplicitArray选项设置为false,那么当它解析时,它将不会在您不期望的地方创建那些数组。如下所示:

代码语言:javascript
复制
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);

此外,如果去掉前缀,它会变得更加整洁:

代码语言:javascript
复制
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);```
票数 4
EN

Stack Overflow用户

发布于 2017-08-10 01:32:49

var custFName = result['soapenv:Envelope']['soapenv:Body'][0]['int:realURLHe‌​re'][0]['response'][‌​0]['cust'][0]['fName‌​'];

这解决了我的问题。我的努力与这个响应有关,所有的东西都在一个数组中。如果有更简单的方法或更动态的方式,请让我知道。但就目前而言,这是可行的。

票数 2
EN

Stack Overflow用户

发布于 2017-08-10 18:01:36

尝尝这个

代码语言:javascript
复制
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))

哪种打印输出

代码语言:javascript
复制
{
  "fname": "hello world"
}

您可以使用result.fname进行访问

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45594547

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档