How to use the parseString function from xml2js

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

xml2js.parseString is a function that converts XML data to JavaScript objects.

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);
fork icon238
star icon0
watch icon89

+ 3 other calls in file

668
669
670
671
672
673
674
675
676
677
} else {
   var content_type = response.headers['content-type'];
   var response_text = name + " N/A";

   if (content_type == 'application/xml;charset=UTF-8') {
      xml2js.parseString(body, {
         tagNameProcessors: [settingsApi.stripPrefix],
         attrNameProcessors: [settingsApi.stripPrefix]
      }, function(err, result) {
         if (err) {
fork icon29
star icon68
watch icon15

+ 19 other calls in file

How does xml2js.parseString work?

xml2js.parseString is a method in the xml2js library that converts XML data to a JavaScript object using a callback function, allowing for easy traversal of the XML tree structure. It first takes the XML string, then parses it into a JavaScript object with its properties and values determined by the XML data. The callback function then uses the resulting object to manipulate and work with the data.

112
113
114
115
116
117
118
119
120
121
if (queryContainsVersionId instanceof Error) {
    return callback(queryContainsVersionId);
}

function parseXml(xmlToParse, next) {
    return parseString(xmlToParse, (err, result) => {
        if (err || !result || !result.CompleteMultipartUpload
            || !result.CompleteMultipartUpload.Part) {
            return next(errors.MalformedXML);
        }
fork icon237
star icon0
watch icon89

+ 4 other calls in file

23
24
25
26
27
28
29
30
31
32
 * @param {object} log - logger
 * @param {ServerSideEncryptionInfo~callback} cb - callback
 * @returns {undefined}
 */
function parseEncryptionXml(xml, log, cb) {
    return parseString(xml, (err, parsed) => {
        if (err) {
            log.trace('xml parsing failed', {
                error: err,
                method: 'parseEncryptionXml',
fork icon236
star icon0
watch icon89

Ai Example

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

const xmlString = " John 30 ";

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

In this example, the xmlString variable contains a simple XML string that represents a person's name and age. The xml2js.parseString() function is then called with the xmlString variable as its first argument, and a callback function as its second argument. When xml2js.parseString() is executed, it takes the XML string and parses it into a JavaScript object. If an error occurs during parsing, the err parameter in the callback function will contain information about the error. Otherwise, the result parameter in the callback function will contain the parsed JavaScript object, which in this case will look like this: javascript Copy code

10
11
12
13
14
15
16
17
18
19
function parseXML(xml, log, cb) {
    if (!xml) {
        log.debug('request xml is missing');
        return cb(errors.MalformedXML);
    }
    return xml2js.parseString(xml, (err, result) => {
        if (err) {
            log.debug('request xml is malformed');
            return cb(errors.MalformedXML);
        }
fork icon236
star icon0
watch icon89

138
139
140
141
142
143
144
145
146
147
148
function getAwsParamsBucketNotMatch(objectKey) {
    return { Bucket: awsMismatchBucket, Key: `${bucketName}/${objectKey}` };
}


function assertMpuInitResults(initResult, key, cb) {
    parseString(initResult, (err, json) => {
        assert.equal(err, null, `Error parsing mpu init results: ${err}`);
        assert.strictEqual(json.InitiateMultipartUploadResult
            .Bucket[0], bucketName);
        assert.strictEqual(json.InitiateMultipartUploadResult
fork icon236
star icon0
watch icon89

+ 4 other calls in file

73
74
75
76
77
78
79
80
81
82
if (error) {
  console.error(error);
} else {
  console.log(body);

  xml2js.parseString(body, (err, result) => {
    if (err) {
      console.error(err);
    } else {
      res.json(result)
fork icon0
star icon0
watch icon1

+ 2 other calls in file

7
8
9
10
11
12
13
14
15
16
  pages: [],
  chars: [],
  kernings: []
}

xml2js.parseString(data, function(err, result) {
  if (err)
    throw err
  if (!result.font)
    throw "XML bitmap font doesn't have <font> root"
fork icon0
star icon0
watch icon1

+ 9 other calls in file

70
71
72
73
74
75
76
77
78
79
80
81
if (!fs.existsSync(resourceDir))
    throw new Error(`Path ${resourceDir} does not exist`);


function parseXML(xml) {
    let result;
    xml2js.parseString(xml, {trim: true}, (err, r) => result = r);
    return result;
}


const result = {};
fork icon0
star icon0
watch icon0

+ 4 other calls in file