Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

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

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?

Regards

like image 384
Aramil Avatar asked Feb 25 '14 22:02

Aramil


2 Answers

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:

result.root.time

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

result.root.time[1].$.c

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;
       break;
    }
 }

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: http://goessner.net/articles/JsonPath/

like image 91
helderdarocha Avatar answered Sep 27 '22 20:09

helderdarocha


While Converting XML to JSON , The xml2js maps the attributes to '$'. In case if your attributes key Name does not match with the Child Key Name . You could Merge the Attributes with Child elements . HEnce your JSON looks clean.

xml2js.Parser({ignoreAttrs : false, mergeAttrs : true})

Might Solve your problem.

like image 21
Sumanth Avatar answered Sep 27 '22 22:09

Sumanth