Node.js - Xml 正在解析

Node.js - Xml Parsing

到目前为止,我已经使用了很多 xml 个解析器,但没有一个能帮助我实现我的 objective。我使用的解析器列表是 xml-body-parserxml2jslibxmljsxamel.

我想做以下事情

到现在为止,我只是完成了发送 xml 然后回复 xml,没有其他事情

目前的代码是

var express = require("express"), 
    bodyParser = require('body-parser');

require("body-parser-xml")(bodyParser);
var xml2js = require('xml2js');
var builder = new xml2js.Builder;
var app = express();
var util = require('util');

app.use(bodyParser.xml({

    xmlParseOptions: {
        normalize: false,    
        normalizeTags: false, 
        explicitArray: false
    }
}));


var XmlPosted;
app.post('/users', function (req, res, body) {
    XmlPosted = req.body;
    console.log();
    res.send(builder.buildObject(req.body));
    res.status(200).end();
});

app.listen(4000);

这段代码对我来说工作得很好,在这段代码中完成的事情是

  1. 搜索特定 xml 标签,然后设置新属性
  2. XML 已解析
  3. 已发送更新的响应

    var DOMParser = new (require('xmldom')).DOMParser({ normalizeTags: { default: false } });
    var express = require("express"),
        bodyParser = require('body-parser');
    require("body-parser-xml")(bodyParser);
    var xml2js = require('xml2js');
    var builder = new xml2js.Builder({ standalone: { default: false } });
    var app = express();
    //Options of body-parser-xml module
    
    app.use(bodyParser.xml({
        xmlParseOptions: {
            normalize: false,     // Trim whitespace inside text nodes
            normalizeTags: false, // Transform tags to lowercase
            explicitArray: false // Only put nodes in array if >1
        }
    }));
    
    //Post Method
    app.post('/users', function (req, res, body) {
    
        //Parsing Request.Body
        var document = DOMParser.parseFromString(
            builder.buildObject(req.body).toString()
        );
        //Getting a list of elements whose name is being given
        var node = document.getElementsByTagName("TextView");
    
        //Changing Tag Name of Specific Elements
        for (var i = 0; i < node.length; i++) {
            node[i].tagName = "com.mycompany.projectname.TextView";
        }
        //Responsing Updated Data
        res.send(document.toString());
    });
    app.listen(1000);