javascript Node.js Xml2js 结果来自属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22027776/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Node.js Xml2js result from attribute
提问by Aramil
What am i doing wrong? i am not able to get the value of a tag inside a child attribute. Here is my xml:
我究竟做错了什么?我无法在子属性中获取标签的值。这是我的 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
问候
回答by helderdarocha
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.
您使用的语法允许您导航 JSON 对象的节点。但似乎您正在尝试将其视为 XPath 谓词进行搜索。它不会工作。
With xml2js can obtain an array of time
objects in your code using:
使用 xml2js 可以使用以下方法获取time
代码中的对象数组:
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:
然后,循环比较$.c
值,这就是您获得属性的方式。比如第二个时间元素的属性是:
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:
这就是您需要比较您的领域的数据。当您发现它在time
数组的哪个元素中时,您会得到title
(实际上是一个包含标题的单元素数组),如下所示:
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
使用 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:
如果不想在 JavaScript 中实现循环来比较属性值,可以使用 JSONPath。它的语法不如 XPath 好,但它有效。您必须通过 NPM 获取它并要求:
var jsonpath = require('JSONPath');
And then you can obtain the title
you want with this expression:
然后你可以title
用这个表达式获得你想要的:
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.
[..]
是谓词,?
是使用表达式运行脚本,@.
是访问属性(在 xml2js 对象中表示为$.c
。您当然可以将字符串替换为您的timetag
变量。
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/
您可以在此处阅读有关 JSONPath 的更多信息:http://goessner.net/articles/JsonPath/
回答by Sumanth
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.
在将 XML 转换为 JSON 时,xml2js 将属性映射到“$”。如果您的属性键 Name 与 Child Key Name 不匹配。您可以将属性与子元素合并。因此您的 JSON 看起来很干净。
xml2js.Parser({ignoreAttrs : false, mergeAttrs : true})
xml2js.Parser({ignoreAttrs: false, mergeAttrs: true})
Might Solve your problem.
可能会解决您的问题。