1.0.0 • Published 6 months ago

@hatimsue/xml-core v1.0.0

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

npm Status

xml-core

This is a lightweight JavaScript library designed for building XML documents. It allows you to create XML element representations as instances of classes, where each object can have attributes and child elements added dynamically. The resulting XML can then be output as a string, ready to be displayed on-screen or saved to a file.

This package is intended as an intermediate step for projects that require XML generation, providing a foundation that can be further abstracted for ease of use in more specialized libraries.

Installation

npm install @hatimsue/xml-core

Example

import { XMLCData, XMLComment, XMLElement } from '@hatimsue/xml-core'

const authors = ['J.K. Rowling', 'J.R.R. Tolkien', 'George R.R. Martin']

// Create the root element <Library> with a namespace
const library = new XMLElement( {
    name: 'Library',
    attributes: { location: 'UK' }
} )

// Add a comment
const comment = new XMLComment( 'This is a library XML document' )
library.addChild( comment )

// Create <bk:Books> with the "bk" namespace and the "genre" attribute
const books = new XMLElement( {
    name: 'Books',
    attributes: { genre: 'fantasy' }
} )

// Add <bk:Book> elements for each author
authors.forEach( author => {
    const book = new XMLElement( {
        name: 'Book',
        attributes: { lang: 'en' }
    } )

    // Add Author and Name as child elements
    book.addChild( new XMLElement( { name: 'Author', children: [author] } ) )
    book.addChild( new XMLElement( { name: 'Name', children: [`A book by ${author}`] } ) )

    books.addChild( book )
} )

// Create an ExtraInfo element with CDATA using the XMLCData class
const extraInfo = new XMLElement( { name: 'ExtraInfo' } )
const cdata = new XMLCData( 'Some unparsed <CDATA> content goes here & should not be escaped.' )
extraInfo.addChild( cdata )

// Add the ExtraInfo element to the XML
library.addChild( books )
library.addChild( extraInfo )

// Convert the XML to a pretty-printed format
console.log( library.toPrettyXML() )

Reference

The classes are designed to be simple and easy to read, so it's recommended to refer to them directly in the src/ folder or consult the auto-generated documentation in API.md. Additionally, you can generate the documentation by running npm run doc, which will create the documentation in the docs folder. However, the following table provides some basic usage examples, though it is not exhaustive.

ConceptDescriptionSyntax / ExampleExpected XML Output
Create a tagUse the XMLElement class constructor to create a tag.new XMLElement('Book')<Book/>
Add contentPass strings or nested elements to the tag using methods like addChild.book.addChild('Title')<Book>Title</Book>
Add attributeUse the setAttribute method to add attributes to the tag.book.setAttribute('lang', 'en')<Book lang="en"/>
Chain attributesChain multiple calls to setAttribute to add more attributes.book.setAttribute('lang', 'en').setAttribute('year', '2001')<Book lang="en" year="2001"/>
Add commentCreate a new XMLComment and add it using addChild.book.addChild(new XMLComment('Note here'))<!-- Note here -->
Add CDATA sectionUse the addChild method to insert a XMLCData section.book.addChild(new XMLCData('<tag>'))<![CDATA[<tag>]]>
Single line outputUse the toXML() method to generate a compact XML string.book.toXML()<Book lang="en"><Author>J.K. Rowling</Author></Book>
Multiline XML outputUse the toPrettyXML() method to generate an indented XML string.book.toPrettyXML()Formatted multi-line XML
Root elementStart your XML tree with a root tag using the class constructor.const library = new XMLElement('Library')<Library>...</Library>