wankdanker / node-object-to-xml

Convert any JavaScript object to XML
MIT License
19 stars 8 forks source link

Converts object with child array and attributes, attributes lost. #2

Closed NickolasHu closed 7 years ago

NickolasHu commented 8 years ago

Hi, when converting object bellow. The node with children lost.

{
  "template": {
    "div": {
      "@": {
        "style": "width:137; height:315"
      },
      "#": [
        {
          "div": {
            "@": {
              "style": "width:137; height:141; left:0.0; top:0.0; background-color:#d7d7d7; border-width:2; border-color:#969696"
            }
          }
        },
        {
          "div": {
            "@": {
              "style": "width:137; height:141; left:0.0; top:174.0; background-color:#d7d7d7; border-width:2; border-color:#969696"
            }
          }
        }
      ]
    }
  }
}

Result:

<template>
  <div>
    <div style="width:137; height:141; left:0.0; top:0.0; background-color:#d7d7d7; border-width:2; border-color:#969696" />
  </div>
  <div>
    <div style="width:137; height:141; left:0.0; top:174.0; background-color:#d7d7d7; border-width:2; border-color:#969696" />
  </div>
</template>

Thanks.

flatanimals commented 8 years ago

I believe what you are attempting to do is currently possible.

Assumed desired result:

<template>
  <div style="width:137; height:315">
    <div style="width:137; height:141; left:0.0; top:0.0; background-color:#d7d7d7; border-width:2; border-color:#969696" />
    <div style="width:137; height:141; left:0.0; top:174.0; background-color:#d7d7d7; border-width:2; border-color:#969696" />
  </div>
</template>

Object that generates the above result:

var o2x = require('object-to-xml');

const template = {
  "template": {
    "div": {
      "@": {
        "style": "width:137; height:315"
      },
      "#": {
        "div": [
          {
            "@": {
              "style": "width:137; height:141; left:0.0; top:0.0; background-color:#d7d7d7; border-width:2; border-color:#969696"
            }
          },

          {
            "@": {
              "style": "width:137; height:141; left:0.0; top:174.0; background-color:#d7d7d7; border-width:2; border-color:#969696"
            }
          }
        ]
      }
    }
  }
};

console.log(o2x(template));