javascript 什么是 Node JS 的最佳 JSON 或 JS 对象到 XML 转换器模块

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24309184/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 02:33:26  来源:igfitidea点击:

What's the best JSON or JS object to XML converter module for Node JS

javascriptxmljsonnode.js

提问by Christiaan Westerbeek

I need to build a relatively simple XML document with some hierarchy and some attributes from a JS object. I stand now before choosing one of these modules:

我需要构建一个相对简单的 XML 文档,其中包含来自 JS 对象的一些层次结构和一些属性。我现在站在选择这些模块之一之前:

Which module should I pick?

我应该选择哪个模块?

This question will probably be dismissed as being not a good question, but I don't know how to ask it any differently.

这个问题可能会因为不是一个好问题而被驳回,但我不知道如何以不同的方式提出这个问题。

Disclaimer: I posted the same question as an issue on each repository

免责声明:我在每个存储库上发布了与问题相同的问题

This is the XML that I want to build:

这是我要构建的 XML:

<?xml version="1.0" encoding="UTF-8"?>
<orders>
    <order>
        <order_orderid>123123</order_orderid>
        <order_customerid>345345</order_customerid>
        <order_senhcode>7604</order_senhcode>
        <order_mediacode>qwert</order_mediacode>
        <order_totalshippingcost>0</order_totalshippingcost>
        <order_paymentmethod>GB</order_paymentmethod>
        <order_paymentnumber />
        <order_htmltext />
        <order_comment />
        <shippingmethodid>02</shippingmethodid>
        <order_creditcardnumber />
        <order_creditcardnameholder />
        <order_creditcardexpiredate />
        <order_creditcardsafetycode />
        <order_gifttext />
        <inv_customer>
            <inv_customer_addresstypeid />
            <inv_customer_gendermale>0</inv_customer_gendermale>
            <inv_customer_firstname>qwerty</inv_customer_firstname>
            <inv_customer_initials>Q.W.E</inv_customer_initials>
            <inv_customer_prename />
            <inv_customer_lastname>Qwerty</inv_customer_lastname>
            <inv_customer_company>Some company</inv_customer_company>
            <inv_customer_street>Postbus</inv_customer_street>
            <inv_customer_housenumber>13</inv_customer_housenumber>
            <inv_customer_housenumberadditional />
            <inv_customer_postalcode>1234 AB</inv_customer_postalcode>
            <inv_customer_city>THERE</inv_customer_city>
            <inv_customer_isocodecountry>NL</inv_customer_isocodecountry>
            <inv_customer_email>[email protected]</inv_customer_email>
            <inv_customer_telephone>0168-123456</inv_customer_telephone>
            <inv_customer_mobilenr>06-12345678</inv_customer_mobilenr>
        </inv_customer>
        <orderlines>
            <orderline>
                <orderline_orderrecordid>1234</orderline_orderrecordid>
                <orderline_orderid>8765432</orderline_orderid>
                <orderline_articlenr>164-05-366</orderline_articlenr>
                <orderline_quantity>2</orderline_quantity>
                <orderline_productdescription>Some gift voucher</orderline_productdescription>
                <orderline_price>1233</orderline_price>
            </orderline>
            <orderline>
                <orderline_orderrecordid>5678</orderline_orderrecordid>
                <orderline_orderid>8765432</orderline_orderid>
                <orderline_articlenr>164-05-367</orderline_articlenr>
                <orderline_quantity>3</orderline_quantity>
                <orderline_productdescription>Some other gift voucher</orderline_productdescription>
                <orderline_price>1244</orderline_price>
            </orderline>
        </orderlines>
    </order>
</orders>

回答by Christiaan Westerbeek

I investigated myself

我调查了自己

| author        | davidcalhoun | soldair   | QuickenLoans | michaelkourlas |
|---------------|--------------|-----------|--------------|----------------|
| module        | jstoxml      | node-     | node-        | node-          |
|               |              | jsontoxml | easyxml      | js2xmlparser   |
| Commits       | 31           | 64        | 39           | 61             |
| Recent commit | a year ago   | 2 years ag| 6 months ago | 16 days ago    |
| Contributors  | 2            | 7         | 7            | 6              |
| Issues        | 16           | 19        | 17           | 15             |
| Open Issues   | 7            | 1         | 6            | 1              |
| npm install   |  jstoxml     | not found | easyxml      | js2xmlparser   |
| Dependencies  | None         | None      | elementtree, | None           |
|               |              |           | inflect      |                |
| Throws errors | None         | None      | 3            | 12             |

Then there's the type of objects required to compare

然后是比较所需的对象类型

davidcalhoun/jstoxml:

大卫卡尔霍恩/jstoxml

{
  "_name": 'foo',
  "_content": 'bar',
  "_attrs": {
    "a": 'b',
    "c": 'd'
  }
}

soldair/node-jsontoxml: looks complicated

sellair/node-jsontoxml:看起来很复杂

QuickenLoans/easyxml:

快速贷款/easyxml:

items: [{
    "name": 'one',
    "_id": 1
  }, {
    "name": 'two',
    "_id": 2
  }
]

michaelkourlas/node-js2xmlparser

michaelkourlas/node-js2xmlparser

foo: {
  "#": 'bar',
  "@": {
    a: 'b',
    c: 'd'
  }
}

I think I'll give michaelkourlas's node-js2xmlparser a shot.

我想我会给 michaelkourlas 的 node-js2xmlparser 一个机会。

Update: It seems now that there are 2 more worth mentioning:

更新:现在看来还有两个值得一提的:

The latter is by far the most matured and downloaded on npm. It allows you to build XML in one go or iteratively.

后者是迄今为止在 npm 上最成熟和下载最多的。它允许您一次性或迭代地构建 XML。