0.1.24 • Published 7 years ago

objtree v0.1.24

Weekly downloads
326
License
-
Repository
github
Last release
7 years ago

ObjTree

Overview

Based on the original version is XML.ObjTree developed by Yusuke Kawasaki. ObjTree.js is a parser/generater for XML source code and JavaScript objects. We modified it by testing for node.js and adding xmldom, which emulates browser data structures on the server side. In this way ObjTree.js can be used in server applications and in the browser. We changed nothing in Yusuke's code, though we include his documentation here for convenience.

##Methods

Initialize and return a new ObjTree object.

Example:

xotree = new 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'
           ]
       }
   }
}
xotree.soft_arrays = true; 

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>';

##Install

The source code is available for download from GitHub. Besides that, you can also install using Node Package Manager npm:

npm install objtree

##License

Copyright (c) 2005-2006 Yusuke Kawasaki. All rights reserved. This program is free software; you can redistribute it and/or modify it under the Artistic license. Or whatever license I choose, which I will do instead of keeping this documentation like it is.