Node.js Xml2js来自属性的结果 [英] Node.js Xml2js result from attribute

查看:29
本文介绍了Node.js Xml2js来自属性的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做什么错?我无法获得子属性内标签的值.这是我的xml:

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?

致谢

推荐答案

您使用的语法允许您导航JSON对象的节点.但是似乎您正在尝试将其视为XPath谓词进行搜索.这行不通.

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.

使用xml2js,可以使用以下代码在代码中获取 time 对象的数组:

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

result.root.time

然后,循环比较 $.c 值,这就是您到达属性的方式.例如,第二个时间元素的属性为:

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

这就是您需要比较字段的数据.当您发现它位于 time 数组的哪个元素中时,您将获得 title (实际上是一个包含标题的单元素数组),如下所示:

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] });

使用JSONPath的解决方案

如果您不想在JavaScript中实现循环以比较属性值,则可以使用JSONPath.它的语法不如XPath好,但它可以工作.您将必须通过NPM获取它,并要求:

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');

然后您可以使用此表达式获取所需的 title :

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

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

[..] 是一个谓词,?是运行带有表达式的脚本, @.是访问属性(在xml2js对象中以 $.c 表示.您当然可以将字符串替换为 timetag 变量.

[..] 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.

要运行搜索,请使用:

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] });

您可以在此处了解有关JSONPath的更多信息: http://goessner.net/articles/JsonPath/

You can read more about JSONPath here: http://goessner.net/articles/JsonPath/

这篇关于Node.js Xml2js来自属性的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆