gpt4 book ai didi

xml - Parse.com 云代码评估 xPath?

转载 作者:数据小太阳 更新时间:2023-10-29 02:40:33 25 4
gpt4 key购买 nike

我是 Parse.com 云代码的新手,主要是一名 iOS 开发人员。在 iOS 中,我使用 Hpple,这是一个用于解析 xml 文档的 xPath 求值器。我现在需要在我的云代码中执行此操作。我想知道 Javascript SDK 中是否已经有一种方法可以使用像

这样的表达式来做到这一点

xpath 表达式:

//day[@name='monday']/meal[@name='LUNCH']/counter[@name='Deli']/dish/name

从这个 url 评估这个 xml:

http://64.182.231.116/~spencerf/union_college/Upperclass_Sample_Menu.xml

这将返回字符串“Made to Order Deli Core”、“Caprese Panini Biggie Sandwich”。有没有办法在 Javascript SDK 中执行此操作,或者是否有任何模块可以安装到可以像这样评估 xml 的解析云代码中?

到目前为止我试过的代码是这样的:

Parse.Cloud.define("next", function(request, response) {

var xmlreader = require('cloud/xmlreader.js');

Parse.Cloud.httpRequest({
url: 'http://64.182.231.116/~spencerf/union_college/Upperclass_Sample_Menu.xml',
success: function(httpResponse) {
//console.log(httpResponse.text);
var someXml = httpResponse.text;

xmlreader.read(someXml, function (err, res){
if(err) return console.log(err);

// use .text() to get the content of a node:
console.log( res.dish.text() );

});

},
error: function(httpResponse) {
console.error('Request failed with response code ' + httpResponse.status);
}
});


});

我可以将数据保存在我的变量 someXml 中,我认为它被保存为一个字符串,这可能会干扰解析,因为我无法解析它。我正在使用 xmlReader 来解析它 (https://www.npmjs.org/package/xmlreader) 有它的工作原理和解析语法的链接。我在解析它时遇到了麻烦。理想情况下,我只想在我使用此代码的地方用 xPath 解析它:

    var getElementByXpath = function (path) {
return someXml.evaluate(path, someXml, null, 2, null).stringValue;

};
console.log(getElementByXpath("//day[@name='monday']/meal[@name='LUNCH']/counter[@name='Deli']/dish/name"));

但这不起作用,它给了我这个错误

has no method 'evaluate'
at getElementByXpath (main.js:131:20)
at Object.Parse.Cloud.httpRequest.success (main.js:134:17)
at Object.<anonymous> (<anonymous>:571:19)

在我提供的链接上,我想获取星期一/午餐下的所有名称节点

最佳答案

关于上述评论(用户 dirkk)中提到的可能限制,不确定这在 Parse.com 中是否可行,但也许这可以工作/帮助:

var getElementByXpath = function (path) {
return document.evaluate(path, document, null, 2, null).stringValue;
};
alert(getElementByXpath("//book/title"));

工作演示:Javascript XPath

更新:以上通过 XPath 使用 Javascript 进行 XML 解析已包含在 OP 中。虽然它可以工作,但下一个问题是通过 URL 获取 XML 文件。 OP 不喜欢使用 Ajax/jQuery 的建议解决方案。建议改用 Parse.Cloud.httpRequest。在此处查找引用:Cloud Code Response Object .此外,此处提到的解决方案存在类似问题:Unable to parse data from an xml file in the Parse Cloud using xmlreader in express app在这里:How to make a REST GET request (with authentication) and parse the result in javascript?在这里看看可能有用:Parse Cloud Code

更新:由于问题已经调整,现在测试 Parse Cloud xmlreader 以从 XML 文件中获取星期一午餐名称。由于 xmlreader 的文档不提供直接通过属性值访问节点属性,因此以下解决方案虽然由于嵌套的 for 循环而看起来很难看,但可以工作:

xmlreader.read(someXml, function (err, res){
if(err) return console.log(err);
for(var i = 0; i < res.day.count(); i++){
if (res.day(i).text()) == "monday")
{
for(var j = 0; j < res.day(i).meal.count(); j++){
if (res.day(i).meal(j).text() == "LUNCH")
{
for(var k = 0; k < res.day(i).meal(j).counter.dish; k++){
console.log( res.day(i).meal(j).counter.dish(k).name.text());
}
}
}
}

更新:由于使用 javascript xpath 类函数进行解析是首选,Parse 似乎还没有提供使用 Xpath 进行 XML 解析 - https://parse.com/questions/htmlxml-parser-with-xpath-on-cloud-code底部的声明 - 并建议改为包括适当的 javascript-library 或 node.js - 在此处找到示例:https://temboo.com/nodejs/parsing-xml - 也许这会有所帮助。如果尚未尝试,请检查 https://parse.com/questions/can-we-importrequireinclude-any-other-javascript-files-in-our-cloud-code

关于xml - Parse.com 云代码评估 xPath?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25352700/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com