Unable to parse data from an xml file in the Parse

2019-09-02 04:05发布

My objective is to get elements of an xml file which i already saved on Parse Cloud for my express app. I started coding after referring this for xmlreader and the guide on Parse httpRequest to retrieve parse file. I tested reading xml elements using 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 the 'user' fetch which is not relevant here).

Then i tried to get xml file from parse cloud using httprequest and it also worked.

Now when i combine both these to get to my objective, 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 shown in log main.js 21:11 points to "xmlreader.read". So obviously the xml file being retrieved from cloud is unable to be browsed through by 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>

Note error 'charAt'. I started coding a month before and looked at w3 xml tuts today noon. Please help!

2条回答
戒情不戒烟
2楼-- · 2019-09-02 04:32

You are feeding your Parse.File instance (file) into the XmlReader, instead of the actual string that it wants.

You have the actual file in your thefile variable, so use that instead.

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

查看更多
SAY GOODBYE
3楼-- · 2019-09-02 04:35

Resolved. First of all there was a typo in XMl file. Replace "PId" with "ParentId". But the error relevant to the question was that..like @timothy suggested, the result of the httpRequest method lies in 'thefile' and the file is in 'thefile.buffer'. For xmlreader input use: thefile.buffer.toString(). There was also error in the looping function in main.js as i misplaced the .at(). I'm attaching the final code to Retrieve 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 xml file is

<?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'll surely take care of typos in future.

查看更多
登录 后发表回答