sxml

Simple XML Library for TypeScript (JavaScript)

MIT License

Downloads
1.5K
Stars
16
Committers
2

Simple XML

It's the most simple, concise and eledic library for XML. Just remember that structure, then you'll understand how to use it. If you want to know more about the detailed features, then utilize auto-completion of TypeScript or read the Guide Documents.

declare module "sxml"
{
    export class XML extends std.HashMap<string, XMLList>
    {
        private tag_: string;
        private value_: string;
        private property_map_: std.HashMap<string, string>;
    }
    export class XMLList extends std.Vector<XML>;
}

Installation

NPM Module

Installing SXML in NodeJS is very easy. Just install with the npm

# Install SXML from the NPM module
npm install --save sxml

Usage

In this section, we will study how to parse XML-string and access to members of that XML object. It's very simple and easy. Just remember and consider the principle structure of this SXML.

If you want to know more about the detailed features or how to generate XML, then utilize auto-completion of TypeScript or read the Guide Documents.

example.xml

<?xml version="1.0" encoding="utf-8" ?>
<invoke listener="setMemberList">
    <parameter name="application" type="string">simulation</parameter>
    <parameter name="sequence" type="number">3</parameter>
    <parameter name="memberList" type="XML">
        <memberList>
            <member id="samchon" email="[email protected]" name="Jeongho Nam" />
            <member id="github" email="[email protected]" name="GitHub" />
            <member id="robot" email="[email protected]" name="AlphaGo" />
        </memberList>
    </parameter>
</invoke>

read.ts

import fs = require("fs");
import sxml = require("sxml");

import XML = sxml.XML;
import XMLList = sxml.XMLList;

function main(): void
{
    let str: string = fs.readFileSync("example.xml", "utf8");
    trace(str);
}

function trace(str: string): void
{
    // CREATE AN XML OBJECT BY PARSING CHARACTERS
    let xml: XML = new XML(str);

    //----
    // LIST OF PARAMETER OBJECTS
    //----
    // XML => std.HashMap<string, XMLList>
    // XMLList => std.Vector<XML>
    let xmlList: XMLList = xml.get("parameter");

    console.log("#" + xmlList.size()); // #3
    
    // PROPERTIES & VALUE OF 1ST PARAMETER
    console.log
    (
        xmlList.at(0).getProperty("name"), // "application"
        xmlList.at(0).getProperty("type"),  // "string"
        xmlList.at(0).getValue() // "simulation"
    );

    // PROPERTIES & VALUE OF 2ND PARAMETER
    console.log
    (
        xmlList.at(1).getProperty("name"), // "sequence"
        xmlList.at(1).getProperty("type"), // "number"
        xmlList.at(1).getValue() // "3"
    );

    //----
    // ACCESS TO CHILDREN XML OBJECTS
    //----
    let members: XMLList = xml.get("parameter").at(2)
        .get("memberList").at(0)
        .get("member");

    // PRINT PROPERTIES
    console.log
    (
        members.at(0).getProperty("id"), // "samchon"
        members.at(1).getProperty("email"), // "[email protected]"
        members.at(2).getProperty("name") // "Alphago
    );
}

main();

References