Ninety Percent Muffin

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

    1.0.4 • Public • Published

    Simple XML

    GitHub license npm version Downloads Build Status FOSSA Status Chat on Gitter

    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="samchon@samchon.org" name="Jeongho Nam" />
                <member id="github" email="github@github.com" name="GitHub" />
                <member id="robot" email="google@google.com" 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"), // "github@github.com"
            members.at(2).getProperty("name") // "Alphago
        );
    }
     
    main();

    References

    Keywords

    Install

    npm i sxml

    DownloadsWeekly Downloads

    959

    Version

    1.0.4

    License

    MIT

    Unpacked Size

    36.6 kB

    Total Files

    15

    Last publish

    Collaborators

    • samchon