Make objects with sax
npm install from-sax-to-objectUsing sax to create objects.
- One dependency: sax
- function parse(source: string): Promise
- function pickFirst(root: Element, name: string): Element | null
- function pickAll(root: Element, name: string): Array
- 150 lines of TypeScript
- Tests included
``
const { parse, pickFirst } = require('from-sax-to-object')
const object = parse(
'
)
console.log(pickFirst(object, 'inner'))
console.log(JSON.stringify(object, null, 2))
`
will print
``
{
"name": "inner"
}
and
``
{
"name": "hello",
"children": [
{
"text": "world"
},
{
"name": "inner"
},
{
"text": "foo"
},
{
"name": "inner",
"attrs": {
"id": "2"
}
},
{
"text": "kick"
}
],
"attrs": {
"zip": "zap"
}
}
Returns the root node to the parsed xml.
- Root node is an element node.
- Each node is either an text node or element node.
- Each element node has a nameattrs
- Each element node can have . It is an object with attributes as keys & values.children
- Each element node can have which is a array of nodes.text`
- Each text node carries the text value with key
Looks up the node and its children to find the first element node with the specified name.
Looks up the node and its children to find the all element nodes with the specified name.