26

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:

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 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>a@b.nl</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>
Ozgur Ozcitak
  • 10,409
  • 8
  • 46
  • 56
Christiaan Westerbeek
  • 10,619
  • 13
  • 64
  • 89
  • 1
    If it's simple and the key names are given, why not just do it yourself ? – adeneo Jun 19 '14 at 14:34
  • 1
    **IMO** if you don't have any special request then pick smallest one. Try if it works for you and then live happy with that. – Adriano Repetti Jun 19 '14 at 14:36
  • 1
    @adeneo because it's better to reuse code that has more contributors and are depended by other projects too. I should not reinvent wheels. Even for simple stuff, because requirements always augment later. – Christiaan Westerbeek Jun 19 '14 at 14:38
  • Well, that's up to you, I would just write something that fits whatever I need. If you look at the modules you've listed they are all very simple, it's just a recursive loop putting keys and values from JSON in XML tags. – adeneo Jun 19 '14 at 14:43
  • @ChristiaanWesterbeek You are wrong. I'm with adeneo unless you provide more info on your specific case. – Ingmars Jun 19 '14 at 14:44
  • I provided the specific use case – Christiaan Westerbeek Jun 19 '14 at 14:50
  • The XML you want to build is related, but picking a module has more to do with how each module fits your coding style, if it meets your requirements (do you need validation? do they report errors? callbacks vs promises? etc), and so on... – Matthew Bakaitis Jun 19 '14 at 14:58
  • I added an answer myself that is trying not to be opinionated by comparing objectively. – Christiaan Westerbeek Jun 19 '14 at 15:46

1 Answers1

40

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:

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

soldair/node-jsontoxml: looks complicated

QuickenLoans/easyxml:

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

michaelkourlas/node-js2xmlparser

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

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

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.

Ozgur Ozcitak
  • 10,409
  • 8
  • 46
  • 56
Christiaan Westerbeek
  • 10,619
  • 13
  • 64
  • 89
  • Thank you for this post. I would also find it interesting to see which modules are blocking (most of them) and which ones actually use a callback to call back with the result. Any opinion on that? – Redsandro Aug 25 '15 at 13:47
  • 2
    `xmlbuilder` is now deprecated in favour of `xmlbuilder2`. – AF7 Jul 31 '20 at 10:09