- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
我是 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/
编辑:为什么这被否决了?我真的不知道...顺便说一句 ../不起作用,因为我不想要 Table 的父级但实际上想要 ../td+1 我不知道这是否可能? 嗨,大家好。 我手头有一个相当复杂的问题..
我很难找到需要单击的输入(复选框)元素的 xpath。我正在尝试使用其他跨度元素来定位它。元素包含 Angular 属性,不知道这是否重要? 元素的结构如下: Company name
我正在尝试构建一个包含许多 XPath 作为参数的 DSL。我是 XPath 的新手,我需要一个 XPath 语法中从未使用过的字符,这样我就可以在脚本的一行中分隔 n 个 XPath。我的问题:哪些
使用xpath在父标签内找到特定标签: 输入样例:
我需要构造一个通用XPath来找到正确的节点,其中的标准是日期和时间。例如查找“ 5 May”,“ 12:17:44”的节点 XML具有日期和时间标签。 不方便地,日期标签仅在当天的第一次出现时填充。
我正在尝试获取xPath几个月内两个日期之间的差异。 几天之内我就没问题了(1127) days-from-duration(xs:date('2012-06-17')-xs:date('2009-0
我试图选择一个包含一段文本的元素,但是我不想选择包含该文本加上其他文本的元素。我通常会使用text()='abc def',但是这个特定元素在前后都包含空格。 这是一个示例片段:
亲爱的,您能帮我用这个XPATH吗?可以说我有以下HTML代码 text value1 value2 text 我需要构建一
我正在尝试提取带有排除项的 xpath,但无法执行此操作。 (//div[@class='row site country-names']/following-sibling::div)[1]/di
response.xpath('//*[@id="blah"]//text()') 假设我的html是 This is a simple text foo and this is after tag.
除了那些具有"//ul/li[not(@*)][count(*)=0]"父项的人以外,我需要全部接受。我已经尝试过,但是不幸的是它不起作用。 有谁知道,我该怎么处理? 提前致谢。 最佳答案 我认为您需
我使用XPath的问题是,每当我使用“子字符串”功能时,我只会得到一个匹配项,而我想全部获得它们。 另一个问题是,每当我使用“子字符串”和运算符的组合时它只是行不通(没有匹配项)。 例如:http:/
我正在尝试通过其位置和属性获取项目,但不知道如何做。 我要实现的是将这一点统一起来: Xpath("//h4/a[contains(@href,'#vuln_')]") 还有这个: Xpath
我有一个xpath如下: .//*[text()='Name:']/../child::select | .//*[text()='Name:']/../child::span 但是对我来说,它既不紧
我拼命试图在xpath中组合几个过滤器。假设我的数据如下所示: DELETE 1 This is my title my sh
我想在已经通过 xpath 设置的其他元素中使用 xpath 来指示元素的位置。 下面的一个已经通过 xpath 设置(我没有改变) //Base_Code
是否可以使用xpath直接在括号内抓取信息?还是以后再用正则表达式过滤? HTML如下所示: Product name (UN1QU3 C0D3) 使用以下Xpath表达式,我可以在此中获取所有内容:
我试图使用一个XPath表达式来选择一个节点,该节点的子节点与文档中的另一个节点匹配。 匹配将意味着该节点的所有属性都相同。因此,如果将一个节点与多个属性进行比较,则无法进行单独的属性比较。 作为示例
我想在 XPath 表达式中使用 Iverson 括号(即映射 true => 1,false => 0)。 示例:而不是书写 someNumber+(if(elem1/elem2[@attr='12
是否可以以类似方式选择节点? './tr[position() in (1, 3, 7)]' 我只找到以下解决方案: './tr[position() = 1 or position() = 3 or
我是一名优秀的程序员,十分优秀!