7

I am trying to work with Express and parse an XML body. I am using the bodyparser for text, but cannot get the xml2js to parse my XML into a usable format.

import * as xml2js from 'xml2js';
try {
    const XML:string = '<Pan>0000000000000702203</Pan>';

    xml2js.parseString(XML, {trim: true}, function (err, result) {
        if(err) console.log(err);
        console.log(result); 
    });
} catch (e) {
    console.log(e);
}

either does not work:

try {
    xml2js.parseString(XML, {trim: true}, (err, result) => {
        if(err) console.log(err);
        console.log(result); 
    });
} catch (e) {
    console.log(e);
}

When running this in the VSCode debugger and the code immediate skips over the function and does not process the xml2js.parseString(). err and result never get values.

I have also tried with it using the Parser() class:

const p:xml2js.Parser = new xml2js.Parser();
p.parseString(XML, (err:{}, result:{}) => {
    if(err) console.log(err);
    console.log(result); 
});

p.parseString(XML, function(err:{}, result:{}) {
    if(err) console.log(err);
    console.log(result); 
});

This does the same thing and does not work or populate the err,result.

Update: 2018/10/11: I have tried debuging this, and it appears the sax.parser is working and returning data. I have done the following:

console.log(xml2js.parseString(XML, (err, result) => {
    if (err) {
        console.log(err);
    }
});

and I do get the SaxParser returned as an object in VSCode which I can interrogate and see my results in, but I do not get my callback function called.

Debug:

SAXParser {comment: "", sgmlDecl: "", textNode: "", tagName: "", doctype: "", …}

The xml2js.parseString though is supposed to not return anything, as the definition has this as a void.

6
  • Which version of xml2js are you using. Your example code works fine here. Output: { Pan: '0000000000000702203' } Commented Oct 13, 2018 at 19:58
  • Where did you find the declaration file for xml2js? Commented Oct 13, 2018 at 23:24
  • @softbear the version I have is 0.4.19. I cannot step into the function in Visual Studio code to debug it. Let me try again with just Postman testing it. Which version of the code though worked? Commented Oct 15, 2018 at 0:28
  • 1
    @DerekMahar I simply installed the @types/xml2js using npm Commented Oct 15, 2018 at 0:29
  • 1
    @DerekMahar Yes. With the type definitions and proper coding, it does work. I also found a fast-xml-parser that works quite well with typescript as well. Commented Oct 15, 2018 at 2:15

2 Answers 2

3

import xml2js from 'xml2js';

async parseXml(xmlString: string) {
    return await new Promise((resolve, reject) => xml2js.parseString(xmlString, (err, jsonData) => {
      if (err) {
        reject(err);
      }
      resolve(jsonData);
    }));

Sign up to request clarification or add additional context in comments.

Comments

0

You can try with JSON.stringify(result).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.