Unable to parse data from XML file in parsing cloud using xmlreader in express app

My goal is to get the elements of the xml file that I have already saved in Parse Cloud for my express app. I started coding following the this link for the xmlreader and the Parse httpRequest guide to extract the parse file. I tested reading xml elements with xmlreader like this:

exports.xmlmodule = function(req, res) {
if (Parse.User.current()){
Parse.User.current().fetch().then(function(user){
var xmlreader = require('cloud/xmlreader');

var someXml =     '<dataset>'
        +         '<brands>'
        +            '<TheId>1</TheId>'
        +            '<ParentId></ParentId>'
        +            '<CategoryorProductName>Casuals</CategoryorProductName>'
        +         '</brands>'
        +         '<brands>'
        +            '<TheId>34</TheId>'
        +            '<ParentId>1</ParentId>'
        +            '<CategoryorProductName>Jeans</CategoryorProductName>'
        +         '</brands>'
        +    '</dataset>'


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

// using the .count() and the .at() function, you can loop through nodes with the same         name:
for(var i = 0; i < res.dataset.brands.count(); i++){
    console.log( res.dataset.brands.TheId.at(i).text() );
    console.log( res.dataset.brands.ParentId.at(i).text() );
    console.log( res.dataset.brands.CategoryorProductName.at(i).text() );
}

console.log("");


});


});}
else{ res.render('login',{welcome: 'Please login to continue'});}
 };

      

and it worked. (Please ignore "user choice" which is not relevant here).

Then I tried to get the XML file from the parsing cloud using httprequest and it worked as well.

Now, when I combine both of these objects to get my target, I get an error:

Failed with: TypeError: Object [object Object] has no method 'charAt'
at Object.write (sax.js:918:31)
at Object.exports.read (xmlreader.js:157:12)
at main.js:21:11
at e (Parse.js:2:5101)
at Parse.js:2:4651
at Array.forEach (native)
at Object.x.each.x.forEach [as _arrayEach] (Parse.js:1:665)
at c.extend.resolve (Parse.js:2:4602)
at Object.<anonymous> (<anonymous>:573:17)

      

I used cloud function and here is my main.js

require ('cloud/app.js');
var xmlreader = require('cloud/xmlreader');
Parse.initialize("xxxxx", "xxxxx");

Parse.Cloud.define('myxmlfunction',function(req,res){
Parse.User.current().fetch().then(function(user){
var theid= user.get('StoreId');
var Stores= Parse.Object.extend('Stores');
var query= new Parse.Query(Stores);
query.equalTo('objectId', theid);
query.first().then(function(results){
var xmlfile= results.get('xmlfile');
Parse.Cloud.httpRequest({
url: xmlfile.url()
}).then(function(thefile){

var file = new Parse.File('thexml.xml', {
 base64: thefile.buffer.toString("base64")
});

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

// using the .count() and the .at() function, you can loop through nodes with the same name:
 for(var i = 0; i < res.myrecord.brands.count(); i++){
     console.log( res.myrecord.brands.TheId.at(i).text() );
      console.log( res.myrecord.brands.ParentId.at(i).text() );
     console.log( res.myrecord.brands.CategoryorProductName.at(i).text() );
 }

 console.log("");

 });

 file.save().then(function() {
 res.success();
 }, function(error) {
 res.error(error);
 });
  });
  });
  });
  });

      

Note: The error displayed in log main.js 21:11 points to "xmlreader.read". Thus, it is obvious that the xml file received from the cloud cannot be viewed by the xmlreader.

My xml file:

 <?xml version="1.0" encoding="UTF-8"?>
 <myrecord>
    <brands>
       <TheId>a</TheId>
       <PId > g</PId>          
       <CategoryNameorProductName >Martin</CategoryNameorProductName>
    </brands>
    <brands>
       <TheId>b</TheId>
       <PId > c</PId>           
       <CategoryNameorProductName >Levis</CategoryNameorProductName>
    </brands> 
</myrecord>

      

Pay attention to the 'charAt' error. I started coding a month before and took a look at w3 xml tuts at noon today. Please help!

0


source to share


2 answers


Decided. First of all, there was a typo in the XMl file. Replace "PId" with "ParentId". But the error related to the question was that ... like @timothy, the result of the httpRequest method is in "file" and the file is in "thefile.buffer". For xmlreader input use: thefile.buffer.toString (). There was also a bug in the looping function in main.js as I lost the .at () value. I am attaching the final code to Extract XML file from Parse Cloud and get its node elements using xmlreader

require ('cloud/app.js');
var xmlreader = require('cloud/xmlreader');
Parse.initialize("xxxx", "xxxx");

Parse.Cloud.define('myxmlfunction',function(req,res){
Parse.User.current().fetch().then(function(user){
var theid= user.get('StoreId');
var Stores= Parse.Object.extend('Stores');
var query= new Parse.Query(Stores);
query.equalTo('objectId', theid);
query.first().then(function(results){
var xmlfile= results.get('xmlfile');
Parse.Cloud.httpRequest({
url: xmlfile.url()
}).then(function(thefile){

xmlreader.read(thefile.buffer.toString(), function (err, res){
if(err) return console.log(err);

// using the .count() and the .at() function, you can loop through nodes with the same name:
 for(var i = 0; i < res.myrecord.brands.count(); i++){
    console.log( res.myrecord.brands.at(i).TheId.text() );
    console.log( res.myrecord.brands.at(i).ParentId.text() );
    console.log( res.myrecord.brands.at(i).CategoryNameorProductName.text() );
}

console.log("");

});


}).then(function() {
res.success();
}, function(error) {
res.error(error);
 });
});
});
});

      

and the xml file

<?xml version="1.0" encoding="UTF-8"?>
<myrecord>
    <brands>
       <TheId>a</TheId>
       <ParentId > g</ParentId>          
       <CategoryNameorProductName >Martin</CategoryNameorProductName>
    </brands>
    <brands>
       <TheId>b</TheId>
       <ParentId > c</ParentId>           
       <CategoryNameorProductName >Levis</CategoryNameorProductName>
    </brands> 
</myrecord>

      



And the log result:

Input: {}
Result: undefined
I2014-07-29T05:56:42.879Z] Levis
I2014-07-29T05:56:42.880Z]  c
I2014-07-29T05:56:42.881Z] a
I2014-07-29T05:56:42.881Z] b
I2014-07-29T05:56:42.881Z] Martin
I2014-07-29T05:56:42.884Z] 
I2014-07-29T05:56:42.885Z]  g

      

I will definitely take care of the typos in the future.

+1


source


You are loading your Parse.File ( file

) instance into the XmlReader instead of the actual string it wants.

You have the actual file in your variable thefile

, so use that.



The Parse.File class is intended when you want to save a file to the cloud and store a pointer to it in the class.

0


source







All Articles