xml-printer
Converts XML AST structures (like the ones generated by xml-reader) to XML strings.
It also provides utility functions to escape XML text and attributes.
AST structure
/** * typedef * @property * @property * @property * @property * @property * @property */
Install
npm install --save xml-printer
Example
; const ast = name: 'greeting' type: 'element' value: '' attributes: time: '2016-01-02' children: name: '' type: 'text' value: 'Hello!' attributes: {} children: ; console;// <greeting time="2016-01-02">Hello!</greeting>
You can easily generate ASTs from text using xml-reader:
; const ast = XmlReader;// returns the AST from the previous example
Options
Pass an options object to the printer function to customize result
; const ast = /* see previous example */ ; console;// <greeting time='2016-01-02'>Hello!</greeting>
Available options
escapeAttributes
: boolean (default:true
) Escapes attributes.escapeText
: boolean (default:true
) Escapes text.selfClose
: boolean (default:true
) Self-close empty elements.quote
: string (default:"
) Quote character, usually"
or'
.
Utilities
This module exports some utility functions which can be useful if you want to escape attributes or text by your own:
escapeXmlText(text: string) => string
; console;// <![CDATA[escape <this>]]>
escapeXmlAttribute(text: string) => string
; console;// escape "this"
License
MIT