Skip to content

Conversion From Object

Ozgur Ozcitak edited this page Dec 10, 2013 · 19 revisions

Child nodes can also be created by passing an object to the element or create functions. Here is an example:

var obj = {
  person: {
    name: "John",
    '@age': 35,
    address: {
      city: "Istanbul"
    },
    '#list': [
      { phone: { '#text': "555-1234", '@type': 'home' } }, 
      { phone: { '#text': "555-1235", '@type': 'mobile' } }
    ],
    id: function() {
      return 42;
    }
  }
};

var builder = require('xmlbuilder');
var root = builder.create(obj);

The resulting XML will be:

<?xml version="1.0"?>
<person age="35">
  <name>John</name>
  <address>
    <city>Istanbul</city>
  </address>
  <phone type="home">555-1234</phone>
  <phone type="mobile">555-1235</phone>
  <id>42</id>
</person>

Objects

Objects will be expanded recursively and each name-value pair is converted to an XML node. If the value is a function, it will be evaluated and its return value will be the node value.

root.ele({
  person: { 
    id: function() { return 42; },
    name: "John"
  }
});
<person>
  <id>42</id>
  <name>John</name>
</person>

Arrays

Arrays will also be expanded and each array item is converted to an XML node.

root.ele({
  numbers: [ 
    "one", 
    "two",
    three: { "@val": 3 }
  ]
});
<numbers>
  <one/>
  <two/>
  <three val="3"/>
</numbers>

Decorators

XML attributes and special nodes are recognized with decorator strings. For example, if an object name starts with @, it will be converted to an XML attribute. Decorators can be customized while calling the create method. Default decorators are shown below.

var root = xmlbuilder.create(obj, {
  stringify: {
    convertAttKey: '@',
    convertTextKey: '#text',
    convertCDataKey: '#cdata',
    convertCommentKey: '#comment',
    convertRawKey: '#raw',
    convertListKey: '#list'
  }
});

Attribute Decorator

Attribute decorator (default @) marks its name-value pair as an attribute. Those attributes are collected and applied to the parent node.

root.ele({ 
  person: {
    name: "John",
    "@age": 35
  }
});
<person age="35">
  <name>John</name>
</person>

Text Decorator

Text decorator (default #text) marks its value as a text node.

root.ele({ 
  person: {
    name: "John",
    "#text": "Smith"
  }
});
<person>
  <name>John</name>
  Smith
</person>

CData Decorator

CData decorator (default #cdata) marks its value as a CData node.

root.ele({ 
  person: {
    name: "John",
    "#cdata": "Smith"
  }
});
<person>
  <name>John</name>
  <![CDATA[Smith]]>
</person>

Comment Decorator

Comment decorator (default #comment) marks its value as a comment node.

root.ele({ 
  person: {
    name: "John",
    "#comment": "Smith"
  }
});
<person>
  <name>John</name>
  <!-- Smith -->
</person>

Raw Decorator

Raw decorator (default #raw) marks its value as a text node, but its contents will not be escaped.

root.ele({ 
  person: {
    name: "John",
    "#raw": "&<>&"
  }
});
<person>
  <name>John</name>
  &<>&
</person>

List Decorator

List decorator (default #list) marks its value as a list of nodes. In this case, a parent node will not be created; list items will be appended to the immediate parent node.

root.ele({ 
  person: {
    name: "John",
    '#list': [
      { phone: "555-1234" },
      { phone: "555-1235" }
    ]
  }
});
<person>
  <name>John</name>
  <phone>555-1234</phone>
  <phone>555-1235</phone>
</person>
Clone this wiki locally