xast utility to parse from XML.
This package is ESM only:
Node 12+ is needed to use it and it must be import
ed instead of require
d.
npm:
npm install xast-util-from-xml
Say we have the following XML file, example.xml
:
<album id="123">
<name>Born in the U.S.A.</name>
<artist>Bruce Springsteen</artist>
<releasedate>1984-04-06</releasedate>
</album>
And our script, example.js
, looks as follows:
import fs from 'node:fs'
import {fromXml} from 'xast-util-from-xml'
const doc = fs.readFileSync('example.xml')
const tree = fromXml(doc)
console.log(tree)
Now, running node example
yields (positional info removed for brevity):
{
type: 'root',
children: [
{
type: 'element',
name: 'album',
attributes: {id: '123'},
children: [
{type: 'text', value: '\n '},
{
type: 'element',
name: 'name',
attributes: {},
children: [{type: 'text', value: 'Born in the U.S.A.'}]
},
{type: 'text', value: '\n '},
{
type: 'element',
name: 'artist',
attributes: {},
children: [{type: 'text', value: 'Bruce Springsteen'}]
},
{type: 'text', value: '\n '},
{
type: 'element',
name: 'releasedate',
attributes: {},
children: [{type: 'text', value: '1984-04-06'}]
},
{type: 'text', value: '\n'}
]
},
{type: 'text', value: '\n'}
]
}
This package exports the following identifiers: fromXml
.
There is no default export.
Parse XML to a xast tree.
Value to parse (string
or Buffer
in UTF-8).
Root
.
XML can be a dangerous language: don’t trust user-provided data.
xast-util-to-xml
— serialize xast to XMLhast-util-to-xast
— transform hast (html, svg) to xast (xml)xastscript
— create xast trees
See contributing.md
in syntax-tree/.github
for ways to get
started.
See support.md
for ways to get help.
This project has a code of conduct. By interacting with this repository, organization, or community you agree to abide by its terms.