onml
JSONML Library
Last updated 5 months ago by drom .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install onml 
SYNC missed versions from official npm registry.

ONML

NPM version Actions Status

jsonml.org compatible tool set.

Use

Node.js

npm i onml --save
var onml = require('onml');

API

onml.parse() --- onml.p()

The onml.parse() method parses a XML/HTML/SVG string and returns a JavaScript value.

var obj = onml.parse('<text a="5">so me</text>');
console.log(obj);
-->
["text", {a: "5"}, "so me"]

onml.stringify() --- onml.s()

The onml.stringify(array, [indentation]) method converts a JavaScript value to a XML/HTML/SVG string.

var str = onml.stringify(['text', {a: 55}, 'so me'], 2);
console.log(str);
-->
<text a="55">
  so me
</text>

onml.traverse() --- onml.t()

JSONML object traversal tool. See test/traverse.js for more details.

onml.traverse(obj, {
    enter: function (node, parent) {
        ...
    },
    leave: function (node, parent) {
        ...
    }
});

Inside enter and leave functions:

node and parent objects have the following attributes:

  • .name -- tag name
  • .attr -- attributes object
  • .full -- full node array

this will hold additional methods:

  • this.name(string) -- to change the node tag
  • this.skip() -- to skip subtree based on the current node
  • this.remove() -- to remove current node
  • this.replace(array) -- to replace current node
// count divs on enter
var count = 0;
onml.traverse(
    ['b',
        ['div', {a: true},
            ['span',
                'div',
                ['div',
                    ['div', {},
                        ['div', {a: true}]
                    ]
                ],
                ['div', {},
                    ['div']
                ]
            ]
        ]
    ],
    {
        enter: function (node) {
            if (node.name === 'div') {
                count++;
            }
        }
    }
);
console.log(count);
-->
6

Testing

npm test

License

MIT LICENSE.

Current Tags

  • 1.2.0                                ...           latest (5 months ago)

15 Versions

  • 1.2.0                                ...           5 months ago
  • 1.1.0                                ...           9 months ago
  • 1.0.0                                ...           a year ago
  • 0.5.0                                ...           a year ago
  • 0.4.5                                ...           2 years ago
  • 0.4.4                                ...           2 years ago
  • 0.4.3                                ...           2 years ago
  • 0.4.2                                ...           2 years ago
  • 0.4.1                                ...           3 years ago
  • 0.4.0                                ...           3 years ago
  • 0.3.1                                ...           4 years ago
  • 0.3.0                                ...           4 years ago
  • 0.2.0                                ...           4 years ago
  • 0.1.1                                ...           5 years ago
  • 0.1.0                                ...           5 years ago
Maintainers (1)
Downloads
Today 0
This Week 1
This Month 1
Last Day 1
Last Week 3
Last Month 11
Dependencies (1)
Dev Dependencies (6)

Copyright 2014 - 2016 © taobao.org |