Aramil Aramil - 1 year ago 194
Javascript Question

Node.js Xml2js result from attribute

What am i doing wrong? i am not able to get the value of a tag inside a child attribute. Here is my xml:

<time c="00:00:00">
<text>Some Text...</text>
<time c="00:00:01">
<title>Title 2</title>
<text>Some text...</text>

This is what i have done in node:

xml2js = require('xml2js');
fs = require('fs');

var parser = new xml2js.Parser();
var timetag = '00:00:01';

fs.readFile( 'test.xml', function(err, data) {

parser.parseString(data, function (err, result) {

function sendSubs() {

io.sockets.emit('subs', { subs: result.root.time[c=timetag].title });

setInterval(sendSubs, 1000);


Im sure thats a syntax issue but i dont see it!, and its possible to get the values of two childs like title and text?


Answer Source

The syntax you are using allows you to navigate the nodes of a JSON object. But it seems you are trying to search treating it like a XPath predicate. It won't work.

With xml2js can obtain an array of time objects in your code using:


And then, loop comparing the $.c value, which is how you reach the attributes. For example, the attribute of the second time element is:


So that's the data you need to compare your field. When you discover in which element of the time array it's in, you get the title (actually a one-element array containing the title) like this:

var resultArr = [];
for(var i = 0; i < result.root.time.length; i++) {
    if (result.root.time[i].$.c == timetag) {
       resultArr = result.root.time[i].title;

Which you can then send to your socket:

io.sockets.emit('subs', { subs: resultArr[0] });

Solution using JSONPath

If you don't want to implement a loop in JavaScript to compare the attribute values, you can use JSONPath. It's syntax is not as nice as XPath, but it works. You will have to get it via NPM and require:

var jsonpath = require('JSONPath');

And then you can obtain the title you want with this expression:

var expression = "$.root.time[?(@.$.c == '00:00:01')].title[0]";

[..] is a predicate, ? is to run a script with an expression, @. is to access the attribute (which is represented in the xml2js object as $.c. You can of course replace the string with your timetag variable.

To run the search, use:

var resultArr = jsonpath.eval(result, expression);

Which will return an array containing the text you want. Then you can send it wherever you want:

io.sockets.emit('subs', { subs: resultArr[0] });

You can read more about JSONPath here:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download