Skip to content

Instantly share code, notes, and snippets.

@ValchanOficial
Forked from Aravin/xmlParser.ts
Created May 22, 2024 06:22
Show Gist options
  • Select an option

  • Save ValchanOficial/92b298bf7795275c6f85663129f8f10f to your computer and use it in GitHub Desktop.

Select an option

Save ValchanOficial/92b298bf7795275c6f85663129f8f10f to your computer and use it in GitHub Desktop.

Revisions

  1. @Aravin Aravin revised this gist Sep 11, 2018. 1 changed file with 14 additions and 0 deletions.
    14 changes: 14 additions & 0 deletions xmlParser.ts
    Original file line number Diff line number Diff line change
    @@ -32,4 +32,18 @@
    // Result
    toJson(employeeXml);
    toXML(employeeJson);


    ////
    /// SAMPLE OUTPUT
    ////

    { Employee: { name: 'Aravind', age: '24', sex: 'Male' } }

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <Employee>
    <name>Aravind</name>
    <age>24</age>
    <sex>Male</sex>
    </Employee>

  2. @Aravin Aravin created this gist Sep 11, 2018.
    35 changes: 35 additions & 0 deletions xmlParser.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,35 @@
    import { parseString, Builder } from "xml2js";

    // Convert string/XML to JSON
    function toJson(xml: string) {

    parseString(xml, { explicitArray: false }, function(error, result) {
    console.log(result);
    });
    }

    // Convert string/JSON to XML
    function toXML(json: string) {

    const builder = new Builder();
    console.log(builder.buildObject(json));
    }


    // Test Data
    const employeeJson: any = {
    Employee: {
    name: 'Aravind',
    age: 24,
    sex: 'Male'

    }
    };

    const employeeXml = "<Employee><name>Aravind</name><age>24</age><sex>Male</sex></Employee>"


    // Result
    toJson(employeeXml);
    toXML(employeeJson);