oozcitak / xmlbuilder-js

An XML builder for node.js
MIT License
921 stars 110 forks source link

Create XML with ordered elements? #191

Closed spaiz closed 5 years ago

spaiz commented 5 years ago

I need to create an XML with elements in specific order. Is there any way to achieve this using object to xml processing? May be by using some special structure?

// not reliable
const test = {
  root: {
    child1: 'val1',
    child2: 'val2',
  }
};

I can't rely on the order of keys in the object cause:

4.3.3 Object An object is a member of the type Object. It is an unordered collection of properties each of which contains a primitive value, object, or function. A function stored in a property of an object is called a

If not, can I be sure the elements will preserve its order when building XML using a regular API?

const test = builder.create('root', { encoding: 'utf-8' })
  .ele('child1', 'val1').up()
  .ele('child2', 'val2').up();

Tnx :)

oozcitak commented 5 years ago

can I be sure the elements will preserve its order when building XML using a regular API?

Yes, nodes are created immediately when ele is called, so the order is preserved.

Is there any way to achieve this using object to xml processing?

Not currently, but I can add a feature to order object keys by alphabetic order or with a user supplied compare function.

spaiz commented 5 years ago

We are validating the XML with XSD where the elements defined to be in particular order, so the alphabetical order will not help.

Anyway, if I can rely on the builder API as you told, it should solve our problem.

Thanks!