xml-parser-xo
Version:
Parse a XML string into a proprietary syntax tree
123 lines (100 loc) • 2.96 kB
Markdown
# xml-parser-xo
An XML parser based on [xml-parser](https://www.npmjs.com/package/xml-parser).
[](https://github.com/chrisbottin/xml-parser/actions/workflows/ci.yml) [](https://npmjs.org/package/xml-parser-xo)
## Installation
```
$ npm install xml-parser-xo
```
## Example
### Usage:
```js
import xmlParser from 'xml-parser-xo';
var xml = `
<!-- Load the stylesheet -->
<foo><![CDATA[some text]]> content</foo>`;
xmlParser(xml);
```
### Output:
```json
{
"declaration": {
"type": "ProcessingInstruction",
"attributes": {"version": "1.0", "encoding": "utf-8"}
},
"root": {
"type": "Element",
"name": "foo",
"attributes": {},
"children": [
{"type": "CDATA", "content": "<![CDATA[some text]]>"},
{"type": "Text", "content": " content"}
]
},
"children": [
{"type": "Comment", "content": "<!-- Load the stylesheet -->"},
{"type": "ProcessingInstruction", "attributes": {"href": "foo.xsl", "type": "text/xsl"}},
{"type": "DocumentType", "content": ""},
{"type": "Text", "content": " content"}
]
}
]
}
```
## Options
- `filter`: Function to filter out unwanted nodes by returning `false`.
- type: `function(node) => boolean`
- default: `() => true`
- `strictMode`: True to throw an error when parsing XML document with invalid content like mismatched closing tags.
- type: `boolean`
- default: `false`
### Usage:
```js
import xmlParser from 'xml-parser-xo';
const xml = `
<!-- Load the stylesheet -->
<foo><![CDATA[some text]]> content</foo>`;
xmlParser(xml, {
filter: (node) => {
return node.type === 'Element' || node.type === 'Text';
}
});
```
### Output:
```json
{
"declaration": {
"type": "ProcessingInstruction",
"attributes": {"version": "1.0", "encoding": "utf-8"}
},
"root": {
"type": "Element",
"name": "foo",
"attributes": {},
"children": [
{"type": "Text", "content": " content"}
]
},
"children": [
{
"type": "Element",
"name": "foo",
"attributes": {},
"children": [
{"type": "Text", "content": " content"}
]
}
]
}
```
## License
MIT