2.0.0 • Published 6 months ago

@hatimsue/xmlgenerator v2.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
6 months ago

npm Status

xmlgenerator

A lightweight JavaScript library for building XML documents using a clean, declarative syntax.

Installation

npm install @hatimsue/xmlgenerator

Overview

This is a complete example showcasing the core features of the xmlgenerator library:

import {XMLGenerator} from '@hatimsue/xmlgenerator'

const _ = new XMLGenerator().builder

// Array of authors to demonstrate loops using spread + map
const authors = ['J.K. Rowling', 'J.R.R. Tolkien', 'George R.R. Martin']

// Start building the XML document
const xml = _.Library
  .$xmlns('http://example.org/library')              // Declare default namespace
  .$xmlns.bk('http://example.org/book')              // Declare prefixed namespace "bk"
  .location('UK')                                     // Add attribute "location"
  (
    _.$comment('This is a library XML document'),     // Insert an XML comment

    _.bk.Books.genre('fantasy')(                      // Create <bk:Books genre="fantasy">
      ...authors.map(author =>                        // Spread multiple <bk:Book> elements
        _.bk.Book.lang('en')(                          // <bk:Book lang="en">
          _.bk.Author(author),                         // Add <bk:Author>Author Name</bk:Author>
          _.bk.Name(`A book by ${author}`)             // Add <bk:Name>Title</bk:Name>
        )
      )
    ),

    _.bk.ExtraInfo(                                   // Another element with CDATA section
      _.$cdata('Some unparsed <CDATA> content goes here & should not be escaped.')
    )
  )

// Pretty-print the final XML
console.log(xml.$toPrettyXML())

Examples

import {XMLGenerator} from '@hatimsue/xmlgenerator'

const _ = new XMLGenerator().builder

const xmlDocument = _.Books(
  _.Book(
    _.Author('J.K. Rowling'),
    _.Name('Harry Potter and the Philosopher\'s Stone')
  )
)

console.log(xmlDocument.$toPrettyXML())
/**
 * <Books>
 *   <Book>
 *     <Author>J.K. Rowling</Author>
 *     <Name>Harry Potter and the Philosopher's Stone</Name>
 *   </Book>
 * </Books>
 */
import {XMLGenerator} from '@hatimsue/xmlgenerator'

const _ = new XMLGenerator().builder

const xmlDocument = 
_.Books.category('fiction').country('UK')(
  _.Book.lang('en')(
    _.Author('J.K. Rowling'),
    _.Name('Harry Potter and the Philosopher\'s Stone')
  )
)

console.log(xmlDocument.$toPrettyXML())
/**
 * <Books category="fiction" country="UK">
 *   <Book lang="en">
 *     <Author>J.K. Rowling</Author>
 *     <Name>Harry Potter and the Philosopher's Stone</Name>
 *   </Book>
 * </Books>
 */
import {XMLGenerator} from '@hatimsue/xmlgenerator'

const _ = new XMLGenerator().builder

const xmlDocument = 
_.Books
  .$xmlns('http://example.org/books') // default namespace
  .$xmlns.bk('http://example.org/book') // prefix namespace
  .$xmlns.ss('http://example.org/ss')  // prefix namespace
  .category('fiction')
  .country('UK')(
    _.bk.Book.lang('en')(
      _.bk.Author('J.K. Rowling'),
      _.bk.Name('Harry Potter and the Philosopher\'s Stone')
    )
)

console.log(xmlDocument.$toPrettyXML())
/**
 * <Books xmlns="http://example.org/books" xmlns:bk="http://example.org/book" category="fiction" country="UK">
 *   <bk:Book lang="en">
 *     <bk:Author>J.K. Rowling</bk:Author>
 *     <bk:Name>Harry Potter and the Philosopher's Stone</bk:Name>
 *   </bk:Book>
 * </Books>
 */

Reference

ConceptDescriptionSyntax / ExampleExpected XML Output
Create a tagUse a method named after the tag to create it. Must be called with ()._.Book()<Book/>
Add contentPass strings or nested tags as arguments inside the parentheses._.Book('Title')<Book>Title</Book>
Add attributeChain attribute methods before calling the tag with ()._.Book.lang('en')()<Book lang="en"/>
Chain attributesChain multiple attribute methods before final ()._.Book.lang('en').year('2001')()<Book lang="en" year="2001"/>
Add default namespaceCall _$xmlns(url) on the element before ()._.Books._$xmlns('http://example.org')()<Books xmlns="http://example.org"/>
Add namespaced prefixUse _$xmlns.prefix(url) to declare namespaced prefixes._.Books._$xmlns.lib('http://example.org/lib')()<Books xmlns:lib="http://example.org/lib"/>
Use namespaced tagsUse prefix as property (e.g., _.lib.TagName()) after declaring it._.lib.Book()<lib:Book/>
Add commentInsert with _.$comment('text') as a child element._.$comment('Note here')<!-- Note here -->
Add CDATA sectionUse _.$cdata('value') to wrap raw XML content safely._.$cdata('<tag>')<![CDATA[<tag>]]>
Use a loop/dynamic contentUse spread syntax with .map() or other loops that return tag arrays....['a','b'].map(n => _.Name(n))<Name>a</Name>\n<Name>b</Name>
Single line outputUse $toXML() to generate compact XML string.element.$toXML()<Book><Author>J.K.</Author></Book>
Multiline XML outputUse $toPrettyXML() to output indented XML for readability.element.$toPrettyXML()Formatted multi-line XML
Root elementStart your XML tree from a top-level tag like _.Library(...).const xml = _.Library(...)<Library>...</Library>
Output XMLLog or return the result from $toXML() or $toPrettyXML().console.log(xml.$toPrettyXML())Console output of the XML
2.0.0

6 months ago

1.0.2

6 months ago

1.0.1

7 months ago

1.0.0

7 months ago