0

I want to convert an XML String into JSON so as to fetch some user details. I am using xml2js library. Below is my typescript code:

typescript.ts

sendXML(){
    console.log("Inside sendXML method")
    console.log(this.XMLAsString)
    this.parseXML(this.XMLAsString);
}

parseXML(XMLAsString){
    var result;
    var parser=require('xml2js');
    parser.Parser().parseString(XMLAsString, (err, r)=>{result=r});
    console.log(result);
  }

ItrAgainstOrderFilTypeComponent.html

<button class="largeButton primaryButton" [disabled]="proceedToVerification" (click)="sendXML()">Proceed to Submit</button>

The sendXML() calls parseXML() function which parses the provided String XMLAsString. The parseXML() takes the String as a parameter and should print its equivalent JSON.

But when i try to run this code, i get the following error: ItrAgainstOrderFilTypeComponent.html:112 ERROR ReferenceError: global is not defined

This is the first time i am using xml2js lib.How do i resolve this? Also, why is it showing an error in the html file (Error is in the html provided above)?

Samarth Saxena
  • 175
  • 3
  • 18

1 Answers1

0

It looks like you're trying to require a package inside your browser, it's usally the way to import packages in a Node.js script.

You may need to find another library or use Browserify.

Mistermatt
  • 556
  • 9
  • 16