Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
390 views
in Technique[技术] by (71.8m points)

javascript - 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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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/


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...