How to use the parseStringPromise function from xml2js

Find comprehensive JavaScript xml2js.parseStringPromise code examples handpicked from public code repositorys.

xml2js.parseStringPromise is a function that converts XML data into a JavaScript object and returns a Promise that resolves with the result.

197
198
199
200
201
202
203
204
205
206
  }

  return { command: cmd, output }
} else if (type === "OS10") {
  try {
    let convertTmp = await xml2js.parseStringPromise(data)
    let tmp = {}

    if (convertTmp["rpc-reply"].hasOwnProperty("bulk")) tmp = convertTmp["rpc-reply"].bulk[0].data[0]
    else tmp = convertTmp["rpc-reply"].data[0]
fork icon0
star icon3
watch icon3

206
207
208
209
210
211
212
213
214
215
    }
    return { command: cmd, output };
}
else if (type === "OS10") {
    try {
        let convertTmp = yield xml2js.parseStringPromise(data);
        let tmp = {};
        if (convertTmp["rpc-reply"].hasOwnProperty("bulk"))
            tmp = convertTmp["rpc-reply"].bulk[0].data[0];
        else
fork icon0
star icon3
watch icon3

+ 3 other calls in file

How does xml2js.parseStringPromise work?

xml2js.parseStringPromise() is a method in the xml2js library for Node.js that converts an XML string to a JavaScript object promise, using a promise-based API. The method parses the XML string and returns a promise that resolves to a JavaScript object that represents the parsed data. The parser options can be customized to control how the XML is parsed.

25
26
27
28
29
30
31
32
33
34

const response = await axios.post(url, xmlData, { headers });

console.log(response.data, "data");

const json = await parseStringPromise(response.data);

const agentTickets =
    json["soap:Envelope"]["soap:Body"]["GetTimeSlotResponse"][
        "GetTimeSlotResult"
fork icon0
star icon2
watch icon1

+ 49 other calls in file

196
197
198
199
200
201
202
203
204
205
206
207
208
async function getFrontChannelLogoutClientKey(request) {


    const buffer = Buffer.from(request, 'base64');
    const xmlData = zlib.inflateRawSync(buffer).toString();


    return xml2js.parseStringPromise(xmlData).then((data) => {


        return data['saml2p:LogoutRequest']
            ['saml2:Issuer'][0]['_'];
    });
fork icon0
star icon1
watch icon4

+ 2 other calls in file

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
13
const xml2js = require("xml2js");

const xml = `

John
30

`;

(async () => {
  const result = await xml2js.parseStringPromise(xml);
  console.log(result);
})();

In this example, we're parsing an XML string that contains information about a person. We're using the parseStringPromise function of the xml2js module to parse the XML and convert it to a JavaScript object. Once the parsing is complete, we're logging the resulting object to the console.

6
7
8
9
10
11
12
13
14
15
16


async function validateTicket(ticket, got) {
    try {
        let response = await got.get(`http://sso:3000/validate?ticket=${ticket}&service=${service}`)
        let data = await response.body
        let xml = await xml2js.parseStringPromise(data, { explicitArray: false })
        return xml.response ? xml.response.authenticationSuccess : undefined
    } catch {
        return undefined
    }
fork icon0
star icon1
watch icon1

+ 8 other calls in file