xml.parse(document)returns the document's top element
xml.write(element, { indent })returns the element and all inside it as text
document is text, bytes or a blob. Text is read as it is. Bytes and a blob are decoded from the character set that their XML declaration names.
Options:
indent spaces of indent for each level; default 0, which writes one line
An element is plain data with these fields.
tagits name as written, with any prefix ("dc:creator")
attrsa map of its attributes; a namespace declaration ("xmlns:dc") is an attribute
textall the text inside it, its children's included, with entities decoded
childrenthe elements directly inside it
- parse leaves comments out.
- write takes the same fields, and attrs, text and children may be omitted.
- write writes an element's children, or its text when it has none.
- write escapes text and attribute values, each of which is text, a number, true or false.
Errors:
bad_input the document is not well formed (the message gives the line), or
a tag or an attribute has a name that XML does not allow
Needs nothing.
This script parses a document and writes another.
const feed = xml.parse('<feed lang="en"><entry><title>One</title></entry></feed>')
feed.attrs.lang // gives "en"
feed.children[0].children[0].text // gives "One"
xml.write({ tag: "order", attrs: { id: 7 }, children: [{ tag: "item", text: "tea & cake" }] })
// gives '<order id="7"><item>tea & cake</item></order>'