1

I am using xml2js

 var fs = require('fs'),
 xml2js = require('xml2js');
 var parser = new xml2js.Parser();
 fs.readFile(path, function(err, data) {
         parser.parseString(data, function (err, result) {
     console.dir(result['QCapsule']['AlarmLog'][0]['AlarmLogItem'][0].AlarmCode);                              });
 });

My xml file looks like

<QCapsule id="1" name="ALARMS">
 ....
 .....
<AlarmLog itemCount="1">
<AlarmLogItem listIndex="0">
  <EntryType>1</EntryType>
  <AlarmCode>180</AlarmCode>
   ....
   ....

The output for

  result['QCapsule']['AlarmLog'][0]['AlarmLogItem'][0].AlarmCode

says [ '180' ]. Is there a way to get the vale without the [] without using string functions. Or is there a better xml parser for nodejs.

2
  • 1
    If I understand your question correctly you just want '180'. If it is right, just access the array at index 0. Commented Mar 13, 2018 at 15:14
  • thank you result['QCapsule']['AlarmLog'][0]['AlarmLogItem'][0].AlarmCode[0] worked..It gave me '180'. If I want only 180,then I guess I have to use string replace and then convert to number? Commented Mar 14, 2018 at 12:17

1 Answer 1

2

You can try:

var path = 'testxml.xml';
var fs = require('fs');
var xml2js = require('xml2js');
var parser = new xml2js.Parser();

var xml = fs.readFileSync(path);

parser.parseString(xml, function (err, result) {
    if (err) {
        console.error('xml2js.parseString: Error occurred: ', err);
    } else {
        console.log(JSON.stringify(result, null, 2));
        console.log('AlarmCode: ', result.QCapsule.AlarmLog[0].AlarmLogItem[0].AlarmCode[0]); 
    }
});

Change the code like so and you don't need those annoying [0] array indexing operators.

var path = 'testxml.xml';
var fs = require('fs');
var xml2js = require('xml2js');
var parser = new xml2js.Parser({explicitArray: false});

var xml = fs.readFileSync(path);

parser.parseString(xml, function (err, result) {
    if (err) {
        console.error('xml2js.parseString: Error occurred: ', err);
    } else {
        console.log(JSON.stringify(result, null, 2));
        console.log('AlarmCode: ', result.QCapsule.AlarmLog.AlarmLogItem[0].AlarmCode);
    }
});

Bear in mind of course that if you do have multiple child elements in the XML data, you'll need to use the indexing operator.

There are some other useful options in the Options section of the xml2js.

And I think xml2js is a pretty nice XML parser, it does a lot!

I tested with Xml that looks like this:

<QCapsule id="1" name="ALARMS">
    <AlarmLog itemCount="2">
        <AlarmLogItem listIndex="0">
            <EntryType>1</EntryType>
            <AlarmCode>180</AlarmCode>
        </AlarmLogItem>
        <AlarmLogItem listIndex="1">
            <EntryType>1</EntryType>
            <AlarmCode>101</AlarmCode>
        </AlarmLogItem>
    </AlarmLog>
</QCapsule>

Also useful to have a print all alarms function:

var printAllAlarms = function(alarmLogItems) {

    console.log(`PrintAllAlarms: Total alarm(s): ${alarmLogItems.length}`);
    alarmLogItems.forEach((alarmLog) => { ;
        console.log(`AlarmLogItem: EntryType: ${alarmLog.EntryType} AlarmCode: ${alarmLog.AlarmCode}`);
    });
}

printAllAlarms(result.QCapsule.AlarmLog.AlarmLogItem);
Sign up to request clarification or add additional context in comments.

1 Comment

thanks for this answer, I'm using it for a Visual Studio Code extension for XML processing

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.