XML parser for Deno
Basic usage
import { parse } from "https://deno.land/x/xml/mod.ts";
console.log(parse(`
<root>
<text>hello</text>
<array>world</array>
<array>monde</array>
<array>δΈη</array>
<array>π</array>
<number>42</number>
<boolean>true</boolean>
<complex attribute="value">content</complex>
</root>
`));
/*
Same nodes are grouped into arrays, while numbers and booleans are auto-parsed (can be disabled)
Nodes with attributes will not be flattened and you'll be able to access them with "@" prefix while
text nodes are available through "$" key
{
text:"hello",
array:["world", "monde", "δΈη", "π"],
number:42,
boolean:true,
complex:{
"@attribute":"value",
$:"content",
}
}
*/
import { stringify } from "https://deno.land/x/xml/mod.ts";
console.log(stringify({
root: {
text: "hello",
array: ["world", "monde", "δΈη", "π"],
number: 42,
boolean: true,
complex: {
"@attribute": "value",
$: "content",
},
},
}));
Features
Follow XML.com's Converting between XML and JSON patterns.
- Support basic XML (tags, self-closed tags, nested tags, attributes, ...)
- Support
XML.parse
andXML.stringify
- Support
<?xml ?>
prolog declaration - Support
<!DOCTYPE>
declaration - Support
<![CDATA[ ]]
strings - Support XML entities (
&
,&
,&
, ...) - Support auto-conversion of primitives (strings, booleans, numbers, null, ...)
- Auto-group nodes into arrays when same tag is used
- Auto-unwrap nodes when it only has text content
How reliable is deno.land/x/xml
? Check parse tests and
stringify tests π§ͺ
Limitations
- Comments are stripped and cannot be recovered
- When using mixed content of texts and child nodes, text node will be stripped and cannot be recovered
- When using mixed group of nodes,
XML.stringify(XML.parse()))
may result in different order- Example:
<a><b/><c/><b/></a>
will result in<a><b/><b/><c/></a>
- This may or may not be acceptable depending on your use case
- Example:
Revivers
By default, node contents will be converted to:
null
when empty, unlessemptyToNull = false
number
when matching finite numbers, unlessreviveNumbers = false
boolean
when matchingtrue
orfalse
(case insensitive), unlessreviveBooleans = false
XML entities (e.g. &
, &
, &
, ...) will be unescaped
automatically.