How to use xml2js

Comprehensive xml2js code examples:

How to use xml2js.default:

94
95
96
97
98
99
100
101
102
103
if (responseBody && responseBody.length >= MaxContentLengthBytes) {
    throw new Error(`Response body is too large for Coda. Body is ${responseBody.length} bytes.`);
}
try {
    if (isXmlContentType(response.headers['content-type']) && !request.isBinaryResponse) {
        responseBody = await xml2js_1.default.parseStringPromise(responseBody, { explicitRoot: false });
    }
    else {
        responseBody = JSON.parse(responseBody);
    }

How to use xml2js.Builder:

357
358
359
360
361
362
363
364
365
366
// console.log("protojson:")
// console.log(protojson)

let xmlDetail
try {
  xmlDetail = new xml2js.Builder(options).buildObject(cotjson.event.detail)
  protojson.cotEvent.xmlDetail = xmlDetail.replace(`<${options.rootName}>`, '').replace(`</${options.rootName}>`, '')
} catch (err) {
  console.error("Error converting detail to xmlDetail:")
  console.error(err);

How to use xml2js.parseString:

248
249
250
251
252
253
254
255
256
257
};
async.waterfall([
    next => bucketPut(authInfo, testPutBucketRequest, log, next),
    (corsHeaders, next) => initiateMultipartUpload(authInfo,
        initiateRequest, log, next),
    (result, corsHeaders, next) => parseString(result, next),
    (json, next) => {
        const testUploadId =
        json.InitiateMultipartUploadResult.UploadId[0];
        const md5Hash = crypto.createHash('md5').update(partBody);

How to use xml2js.Parser:

259
260
261
262
263
264
265
266
267
268
var xmlFile = objectsPath + '/' + folderName + '/' + identityFolderName + '/target/target.xml';
var jsonFile = objectsPath + '/' + folderName + '/' + identityFolderName + '/object.json';

if (fs.existsSync(xmlFile)) {
    var resultXML = '';
    xml2js.Parser().parseString(fs.readFileSync(xmlFile, 'utf8'),
        function (err, result) {
            for (var first in result) {
                for (var secondFirst in result[first].Tracking[0]) {
                    resultXML = result[first].Tracking[0][secondFirst][0].$.name;

How to use xml2js.parseStringPromise:

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]