xml-formatter
    TypeScript icon, indicating that this package has built-in type declarations

    2.4.1 • Public • Published

    xml-formatter

    Converts XML into a human readable format (pretty print) while respecting the xml:space attribute.

    This module can also be used on the browser using the browserified version with a small footprint (8KB file size).

    Travis CI status

    NPM

    Installation

    $ npm install xml-formatter
    

    Example

    JavaScript:

    var format = require('xml-formatter');
    var xml = '<root><content><p xml:space="preserve">This is <b>some</b> content.</content></p>';
    
    var formattedXml = format(xml);
    console.log(formattedXml);

    Output:

    <root>
        <content>
            <p xml:space="preserve">This is <b>some</b> content.</p>
        </content>
    </root>

    Options

    JavaScript:

    var format = require('xml-formatter');
    var xml = '<root><!-- content --><content><p>This is <b>some</b> content.</content></p>';
    
    var formattedXml = format(xml, {
        indentation: '  ', 
        filter: (node) => node.type !== 'Comment', 
        collapseContent: true, 
        lineSeparator: '\n'
    });
    
    console.log(formattedXml);

    Output:

    <root>
      <content>
        <p>This is <b>some</b> content.</p>
      </content>
    </root>
    • filter (function(node)) Function to filter out unwanted nodes by returning false.
    • indentation (String, default=' ') The value used for indentation.
    • collapseContent (Boolean, default=false] True to keep content in the same line as the element. Only works if element contains at least one text node
    • lineSeparator (String, default=\r\n) Specify the line separator to use
    • whiteSpaceAtEndOfSelfclosingTag (Boolean, default=false) to either end ad self closing tag with <tag/> or <tag />

    On The Browser

    Page:

    <script type="text/javascript" src="dist/browser/xml-formatter.js"></script>

    Usage:

    var format = require('xml-formatter');
    var xml = '<root><content><p xml:space="preserve">This is <b>some</b> content.</content></p>';
    
    var formattedXml = format(xml);
    console.log(formattedXml);

    Output:

    <root>
        <content>
            <p xml:space="preserve">This is <b>some</b> content.</p>
        </content>
    </root>

    License

    MIT

    Install

    npm i xml-formatter

    DownloadsWeekly Downloads

    85,921

    Version

    2.4.1

    License

    MIT

    Unpacked Size

    21.9 kB

    Total Files

    6

    Last publish

    Collaborators

    • chrisbottin