I am new to using Parse.com cloud code, and am mainly a iOS developer. In iOS I use Hpple, an xPath evaluator to parse an xml document. I now need to do this in my cloud code. I was wondering if there is already a way in the Javascript SDK to do this with expressions like
xpath expression:
//day[@name='monday']/meal[@name='LUNCH']/counter[@name='Deli']/dish/name
to evaluate this xml from this url:
http://64.182.231.116/~spencerf/union_college/Upperclass_Sample_Menu.xml
This would return the strings "Made to Order Deli Core", "Caprese Panini Biggie Sandwich". Is there a way to do this in the Javascript SDK or are there any modules that I can install into parse's cloud code that can evaluate xml like this?
The code I have tried so far is this:
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);
}
});
});
I can get the data saved in my variable someXml, I think it is saved as a string, which is maybe messing with the parsing because I am unable to parse it. I am using xmlReader to parse it (https://www.npmjs.org/package/xmlreader) there is the link with how it works and the syntax for parsing. I am having trouble parsing it though. Ideally I would like to just parse it with xPath where I was using this code:
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"));
But this was not working, it gave me this error
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)
On the link I provided I would like to get all the name nodes under monday/lunch
Not sure if this is possible in Parse.com regarding possible restrictions mentioned in above comments (user dirkk), but maybe this could work / help:
var getElementByXpath = function (path) {
return document.evaluate(path, document, null, 2, null).stringValue;
};
alert(getElementByXpath("//book/title"));
Working Demo: Javascript XPath
Update: above XML-parsing with Javascript by XPath was included in OP. Though it could work, next issue was to get XML-file by URL. Suggested solution using Ajax / jQuery wasn't preferred by OP. Suggested to use Parse.Cloud.httpRequest instead. Find reference here: Cloud Code Response Object. In addtion, similar problem with solution mentioned here: Unable to parse data from an xml file in the Parse Cloud using xmlreader in express app and here: How to make a REST GET request (with authentication) and parse the result in javascript?
Maybe useful to have a look here: Parse Cloud Code
Update: As question has been adjusted, Parse Cloud xmlreader is now tested to get the monday lunch names from the XML-File. As documentation of xmlreader doesn't offer to access a node attribute by attribute value directly, following solution, though it looks ugly because of nested for-loops, could work:
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());
}
}
}
}
Update: As parsing using javascript xpath-like functions is preferred and Parse doesn't seem to offer yet XML Parsing with Xpath - https://parse.com/questions/htmlxml-parser-with-xpath-on-cloud-code statement at the bottom - and recommends to include appropriate javascript-library or node.js instead - find example here: https://temboo.com/nodejs/parsing-xml - maybe this would help. If not already tried, check https://parse.com/questions/can-we-importrequireinclude-any-other-javascript-files-in-our-cloud-code
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With