Parse.com Cloud Code Rate xPath?

I am new to using Parse.com cloud code and I am mainly an iOS developer. On iOS, I am using Hpple, the xPath evaluator to parse an XML document. Now I need to do this in my cloud code. I was wondering if the Javascript SDK already has a way to do this using 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 will return the lines "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 I can install in the cloud syntax code that can evaluate the xml like this?

The code I've 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 stored in my someXml variable, I think this is stored as a string, which is probably messing around with parsing because I can't parse it. I use xmlReader

( https://www.npmjs.org/package/xmlreader

) to parse it , there is a link to how it works and the syntax to parse it. However, I am having problems with this. Ideally I would like to just parse it with xPath where I used 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 it didn't work, 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)

      

From the link I provided, I would like to get all the name nodes on Monday / lunch

+3


source to share


3 answers


It's good here.



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

      

0


source


Not sure if this is possible at Parse.com regarding the possible limitations mentioned in the comments above (user dirkk), but maybe it might work / help:

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

      

Working Demo: Javascript XPath

Update: The above XML parsing with Javascript from XPath was included in the OP. While it might work, the next problem was getting the XML file from the url. The proposed solution using Ajax / jQuery was not preferred by the OP. Recommended to use Parse.Cloud.httpRequest

. Find the link here: Cloud Code Answer Object . In addition, a similar problem with the solution mentioned here: Unable to parse data from XML file in parsing cloud using xmlreader in express app and here: How to make a REST GET request (with authentication) and parse the result in javascript? It might be helpful to look here: Analysis Cloud Code



Update. As the question was corrected, the Parse Cloud XML file is now being tested to get the lunch lunch names from the XML file. Since the xmlreader documentation does not suggest directly accessing the node attribute by attribute value, the following solution, although it looks ugly due to nested loops, might 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: since parsing using javascript xpath-like functions is preferred and Parse doesn't seem to offer XML parsing with Xpath yet - https://parse.com/questions/htmlxml-parser-with-xpath-on- cloud-code , and recommends including the appropriate javascript library or node.js instead - find an example here: https://temboo.com/nodejs/parsing-xml - Maybe this will help. If you haven't tried it yet, check out https://parse.com/questions/can-we-importrequireinclude-any-other-javascript-files-in-our-cloud-code

+1


source


This should get you started and get started.

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

      

0


source







All Articles