Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/pladaria/xml-printer
Converts XML AST tree structures (like the ones generated by xml-reader) to XML strings.
https://github.com/pladaria/xml-printer
Last synced: about 2 months ago
JSON representation
Converts XML AST tree structures (like the ones generated by xml-reader) to XML strings.
- Host: GitHub
- URL: https://github.com/pladaria/xml-printer
- Owner: pladaria
- License: mit
- Created: 2016-09-24T10:25:29.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2016-09-28T09:40:25.000Z (over 8 years ago)
- Last Synced: 2024-08-08T20:18:10.633Z (5 months ago)
- Language: JavaScript
- Size: 16.6 KB
- Stars: 2
- Watchers: 2
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# xml-printer [![Build Status](https://travis-ci.org/pladaria/xml-printer.svg)](https://travis-ci.org/pladaria/xml-printer) [![Coverage Status](https://coveralls.io/repos/github/pladaria/xml-printer/badge.svg?branch=master)](https://coveralls.io/github/pladaria/xml-printer?branch=master)
Converts XML AST structures (like the ones generated by [xml-reader](https://www.github.com/pladaria/xml-reader)) to XML strings.
It also provides utility functions to escape XML text and attributes.
## AST structure
```javascript
/**
* typedef {Object} XmlNode
* @property {string} name - element name (empty for text nodes)
* @property {string} type - node type ('element' or 'text')
* @property {string} value - value of a text node
* @property {XmlNode} parent - reference to parent node
* @property {Object} attributes - attributes {name: value, ...}
* @property {XmlNode[]} children - array of children nodes
*/
```
## Install```bash
npm install --save xml-printer
```## Example
```javascript
import xmlPrint from 'xml-printer';const ast = {
name: 'greeting',
type: 'element',
value: '',
attributes: {time: '2016-01-02'},
children: [
{
name: '',
type: 'text',
value: 'Hello!',
attributes: {},
children: [],
},
],
};console.log(xmlPrint(ast));
// Hello!
```You can easily generate ASTs from text using [xml-reader](https://www.github.com/pladaria/xml-reader):
```javascript
import XmlReader from 'xml-reader';const ast = XmlReader.parseSync('Hello!');
// returns the AST from the previous example
```## Options
Pass an options object to the printer function to customize result
```javascript
import xmlPrint from 'xml-printer';const ast = { /* see previous example */ };
console.log(xmlPrint(ast, {quote: "'"}));
// Hello!
```### 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
```javascript
import {escapeXmlText} from 'xml-printer';console.log(escapeXmlText('escape '));
// ]]>
```### escapeXmlAttribute(text: string) => string
```javascript
import {escapeXmlAttribute} from 'xml-printer';console.log(escapeXmlAttribute('escape '));
// escape "this"
```## License
MIT