将 XML 处理为 JSON 并在 HTML 中显示特定元素的问题

安德鲁·彼得森

我正在尝试操作来自 html.request 的 XML 数据。我的请求设置正确,响应是正确的 XML。(这是我下面脚本中“console.log(soapreplyx);”的输出。)

<?xml version='1.0' encoding='utf-8'?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
    <soapenv:Body>
        <ns:listCssResponse xmlns:ns="http://www.cisco.com/AXL/API/11.5">
            <return>
                <css uuid="{CA14384C-D04D-39D0-2BAF-01EEDD8E9914}">
                    <description/>
                    <clause>AllPhones</clause>
                    <name>SUBSCRIBE</name>
                </css>
                <css uuid="{97FE9A56-85D7-8198-805C-925E2FB264DD}">
                    <description>CSS for Unity Connection SIP Trunk</description>
                    <clause>AllPhones:Unity_Connection:UCCX</clause>
                    <name>CSS-Unity</name>
                </css>
                <css uuid="{1D61F1AD-8E1C-DC71-C9AD-E37B85DE0F9C}">
                    <description>Internal Only</description>
                    <clause>AllPhones:Unity_Connection:Agents:UCCX</clause>
                    <name>CSS-Internal</name>
                </css>
                <css uuid="{C09EDFA7-4BF7-AF15-483D-54F0D07666A1}">
                    <description>Inbound CSS for Gateways</description>
                    <clause>AllPhones:Unity_Connection:UCCX</clause>
                    <name>CSS-Gateways</name>
                </css>
                <css uuid="{A2BF1C5E-6685-D33A-D8BB-037F4D211DC9}">
                    <description>Local, Internal, and Emergency</description>
                    <clause>AllPhones:Unity_Connection:LAB-PSTN-Sim-911:LAB-PSTN-Sim-Local:Agents:UCCX</clause>
                    <name>CSS-PSTN-Sim-Local</name>
                </css>
                <css uuid="{DF03B400-DA2D-29A7-6460-D0B72D578CD7}">
                    <description>LD, Local, Internal, and Emergency</description>
                    <clause>AllPhones:Unity_Connection:LAB-PSTN-Sim-911:LAB-PSTN-Sim-Local:LAB-PSTN-Sim-LD:Blocked:Agents:UCCX</clause>
                    <name>CSS-PSTN-Sim-LD</name>
                </css>
                <css uuid="{F4626B2A-E3F6-6BF0-78E7-CBB6123091CB}">
                    <description>Intl, LD, Local, Internal, and Emergency</description>
                    <clause>AllPhones:Unity_Connection:Blocked:LAB-PSTN-Sim-911:LAB-PSTN-Sim-Local:LAB-PSTN-Sim-LD:LAB-PSTN-Sim-Intl:Agents:UCCX</clause>
                    <name>CSS-PSTN-Sim-Intl</name>
                </css>
                <css uuid="{70E8D03E-623E-354C-991D-9FB392747291}">
                    <description>Inbound Transform Calling</description>
                    <clause>LAB-Tansform-Calling</clause>
                    <name>CSS-LAB-Transform-Calling</name>
                </css>
                <css uuid="{5FD922AD-9A14-FD17-ACBF-8FAAF2B7326F}">
                    <description>CSS for UCCX Port Group</description>
                    <clause>AllPhones:Agents:Unity_Connection:UCCX</clause>
                    <name>CSS-UCCX</name>
                </css>
            </return>
        </ns:listCssResponse>
    </soapenv:Body>
</soapenv:Envelope>

我正在尝试将该 XML 数据的一部分转换为 JSON,以便我可以在 HTML 表中引用它们。这就是我进入热水的地方。我正在使用库“camaro”来帮助我处理数据并将其转换为 JSON。这是我的输出结果。(这是我下面脚本中“console.log(json);”的输出。)

{ css:
   [ '': { cssdescription: 'CSS for UCCX Port Group',
       cssname: 'CSS-UCCX',
       csspartitions: 'AllPhones:Agents:Unity_Connection:UCCX' } ] }

这是我的脚本:

// MODULES - INCLUDES
var transform = require('camaro');

module.exports = function (app) {
  // FORM - SUBMIT - CUCMMAPPER
  app.post('/cucmmapper/submit', function (req, res) {

    // FORM - DATA COLLECTION
    var cucmpub = req.body.cucmpub;
    var cucmversion = req.body.cucmversion;
    var username = req.body.username;
    var password = req.body.password;

    // JS - VARIABLE DEFINITION
    var authentication = username + ":" + password;
    var soapreplyx = '';
    var cssx = '';
    var spacer = '--------';

    // HTTP.REQUEST - BUILD CALL
    var https = require("https");
    var headers = {
      'SoapAction': 'CUCM:DB ver=' + cucmversion + ' listCss',
      'Authorization': 'Basic ' + new Buffer(authentication).toString('base64'),
      'Content-Type': 'text/xml; charset=utf-8'
    };

    // SOAP - AXL CALL
    var soapBody = new Buffer('<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns="http://www.cisco.com/AXL/API/11.5">' +
      '<soapenv:Header/>' +
      '<soapenv:Body>' +
      '<ns:listCss sequence="?">' +
      '<searchCriteria>' +
      '<name>%</name>' +
      '</searchCriteria>' +
      '<returnedTags uuid="?">' +
      '<name>?</name>' +
      '<description>?</description>' +
      '<clause>?</clause>' +
      '</returnedTags>' +
      '</ns:listCss>' +
      '</soapenv:Body>' +
      '</soapenv:Envelope>');

    // HTTP.REQUEST - OPTIONS
    var options = {
      host: cucmpub, // IP ADDRESS OF CUCM PUBLISHER
      port: 8443, // DEFAULT CISCO SSL PORT
      path: '/axl/', // AXL URL
      method: 'POST', // AXL REQUIREMENT OF POST
      headers: headers, // HEADER VAR
      rejectUnauthorized: false // REQUIRED TO ACCEPT SELF-SIGNED CERTS
    };

    // HTTP.REQUEST - Doesn't seem to need this line, but it might be useful anyway for pooling?
    options.agent = new https.Agent(options);

    // HTTP.REQUEST - OPEN SESSION
    let soapRequest = https.request(options, soapResponse => {
      soapResponse.setEncoding('utf8');
      soapResponse.on('data', chunk => {
        soapreplyx += chunk
      });
      // HTTP.REQUEST - RESULTS + RENDER
      soapResponse.on('end', () => {
        console.log(soapreplyx);
        console.log(spacer);
        const json = transform(soapreplyx, {
          css: ['//css', {
              cssname: 'name',
              cssdescription: 'description',
              csspartitions: 'clause'
          }]
        });
        console.log(json);
        console.log(spacer);
        var cssx = JSON.parse(json);
        console.log(cssx.cssname);
        res.render('cucmmapper-results.html', {
          title: 'CUCM 2.1',
          soapreply: soapreplyx,
          cucmpub: cucmpub,
          css: cssx
        });
      });
    });

    // SOAP - SEND AXL CALL
    soapRequest.write(soapBody);
    soapRequest.end();
  });
}

我正在尝试按照之前发布的问题的答案进行操作。如何提取 json 对象内的 json 对象我认为我很接近,但怀疑我的 JSON 数据不太正确并且不确定如何修复它。

我还是 JS/Node/Express 的新手。我感谢任何人可以提供的任何帮助或指导。谢谢大家!

安德鲁·彼得森

我决定回到 xml2js 并删除我得到的 XML 输出中的一些违规行,并且它工作得非常好。这是我的代码。

// MODULES - INCLUDES
var xml2js = require('xml2js');
var parser = new xml2js.Parser();

module.exports = function (app) {
  // FORM - SUBMIT - CUCMMAPPER
  app.post('/cucmmapper/submit', function (req, res) {

    // FORM - DATA COLLECTION
    var cucmpub = req.body.cucmpub;
    var cucmversion = req.body.cucmversion;
    var username = req.body.username;
    var password = req.body.password;

    // JS - VARIABLE DEFINITION
    var authentication = username + ":" + password;
    var soapreplyx = '';
    var cssx = '';
    var spacer = '-----';
    var rmline1 = '';
    var rmline2 = '';
    var rmline3 = '';
    var rmline4 = '';
    var rmbottomup1 = '';
    var rmbottomup2 = '';
    var rmbottomup3 = '';

    // HTTP.REQUEST - BUILD CALL
    var https = require("https");
    var headers = {
      'SoapAction': 'CUCM:DB ver=' + cucmversion + ' listCss',
      'Authorization': 'Basic ' + new Buffer(authentication).toString('base64'),
      'Content-Type': 'text/xml; charset=utf-8'
    };

    // SOAP - AXL CALL
    var soapBody = new Buffer('<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns="http://www.cisco.com/AXL/API/11.5">' +
      '<soapenv:Header/>' +
      '<soapenv:Body>' +
      '<ns:listCss sequence="?">' +
      '<searchCriteria>' +
      '<name>%</name>' +
      '</searchCriteria>' +
      '<returnedTags uuid="?">' +
      '<name>?</name>' +
      '<description>?</description>' +
      '<clause>?</clause>' +
      '</returnedTags>' +
      '</ns:listCss>' +
      '</soapenv:Body>' +
      '</soapenv:Envelope>');

    // HTTP.REQUEST - OPTIONS
    var options = {
      host: cucmpub, // IP ADDRESS OF CUCM PUBLISHER
      port: 8443, // DEFAULT CISCO SSL PORT
      path: '/axl/', // AXL URL
      method: 'POST', // AXL REQUIREMENT OF POST
      headers: headers, // HEADER VAR
      rejectUnauthorized: false // REQUIRED TO ACCEPT SELF-SIGNED CERTS
    };

    // HTTP.REQUEST - Doesn't seem to need this line, but it might be useful anyway for pooling?
    options.agent = new https.Agent(options);

    // HTTP.REQUEST - OPEN SESSION
    let soapRequest = https.request(options, soapResponse => {
      soapResponse.setEncoding('utf8');
      soapResponse.on('data', chunk => {
        soapreplyx += chunk
      });
      // HTTP.REQUEST - RESULTS + RENDER
      soapResponse.on('end', () => {

        // EDIT - SCRUB XML OUTPUT
        var rmline1 = soapreplyx.replace(/<\?xml\sversion='1\.0'\sencoding='utf-8'\?>/g, '');
        var rmline2 = rmline1.replace(/<soapenv:Envelope\sxmlns:soapenv="http:\/\/schemas.xmlsoap.org\/soap\/envelope\/">/g, '');
        var rmline3 = rmline2.replace(/<soapenv:Body>/g, '');
        var rmline4 = rmline3.replace(/<ns:listCssResponse\sxmlns:ns="http:\/\/www\.cisco\.com\/AXL\/API\/[0-9]*\.[0-9]">/g, '');
        var rmbottomup1 = rmline4.replace(/<\/soapenv:Envelope>/g, '');
        var rmbottomup2 = rmbottomup1.replace(/<\/soapenv:Body>/g, '');
        var xmlscrubbed = rmbottomup2.replace(/<\/ns:listCssResponse>/g, '');
        // console.log(xmlscrubbed);
        // console.log(spacer);

        // XML2JS - TESTING
        parser.parseString(xmlscrubbed, function (err, result) {
          var cssx = result['return']['css'];
        //   console.log(cssx);
        //   console.log(spacer);
          res.render('cucmmapper-results.html', {
            title: 'CUCM 2.0',
            cucmpub: cucmpub,
            cssx: cssx,
            soapreply: soapreplyx,
            xmlscrubbed: xmlscrubbed
          });
        });
      });
    });

    // SOAP - SEND AXL CALL
    soapRequest.write(soapBody);
    soapRequest.end();
  });
}

我最终得到了完美的 JSON,我可以很好地使用它。感谢大家的帮助!

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

TOP 榜单

热门标签

归档