#node-ObjTree.js
node-ObjTree.js is a parser/generater for XML source code and JavaScript object. The original version is XML.ObjTree developed by Yusuke Kawasaki. It is modified by Ronald Ranauro so that it can be used both in the node.js and browser.
##Download
The source code is available for download from GitHub. Besides that, you can also install using Node Package Manager npm:
npm install node-ObjTree
##Documentation
### XML.ObjTree()Initialize and return a new XML.ObjTree object.
Example:
xotree = new XML.ObjTree()
Load an XML document using the supplied string and return its JavaScript object converted.
Example:
objtree_xml = '
<?xml version="1.0" encoding="UTF-8"?>
<family name="Kawasaki">
<father>Yasuhisa</father>
<mother>Chizuko</mother>
<children>
<girl>Shiori</girl>
<boy>Yusuke</boy>
<boy>Kairi</boy>
</children>
</family>';
var xotree_out = xotree.parseXML( objtree_xml );
Result:
xctree_out ==
{
'family': {
'-name': 'Kawasaki',
'father': 'Yasuhisa',
'mother': 'Chizuko',
'children': {
'girl': 'Shiori',
'boy': [
'Yusuke',
'Kairi'
]
}
}
}
- How to get the attribute of an element:
Example:
xotree_out.family['-name'];
Result:
Kawasaki
- How to get the text of an element:
Example:
xotree_out.family.father
xotree_out.family.children.boy[1]
Result:
Yasuhisa
Kairi
Set the prefix character which is inserted before each attribute names. Default is '-'.
Example:
xotree.attr_prefix = '@';
xotree_out = xotree.parseXML( objtree_xml );
xotree_out.family['@name'];
Result:
Kawasaki
Specify a list of element names which should always be forced into an array representation. The default value is null.
Example:
xotree.force_array = ['boy', 'father'];
xotree_out = xotree.parseXML( objtree_xml );
xotree_out.family.father //is an array now
xotree_out.family.father[0]
Result:
['Yasuhisa']
Yasuhisa
xctree_out ==
{
'family': {
'-name': 'Kawasaki',
'father': ['Yasuhisa'],
'mother': 'Chizuko',
'children': {
'girl': 'Shiori',
'boy': [
'Yusuke',
'Kairi'
]
}
}
}
Parse a JavaScript object tree and returns its XML source string generated.
Example:
var xml_out = xotree.writeXML( objtree_json )
Result:
xml_out =='
<?xml version="1.0" encoding="UTF-8"?>
<family name="Kawasaki">
<father>Yasuhisa</father>
<mother>Chizuko</mother>
<children>
<girl>Shiori</girl>
<boy>Yusuke</boy>
<boy>Kairi</boy>
</children>
</family>';