My goal is to extract elements from an XML file that I have already stored on the Parse Cloud for my Express app. I began coding after finding a helpful resource on using XMLReader with XPath on cloud code, as well as a guide on Parse httpRequest for retrieving Parse files. I conducted a test reading XML elements using XMLReader in the following manner:
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);
// utilizing the .count() and the .at() functions, you can iterate 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'});}
};
This approach was successful. (Please disregard the 'user' fetch which is not relevant here).
Next, I attempted to retrieve an XML file from the Parse Cloud using HTTPRequest, which also worked.
However, when I tried merging these two methods to achieve my objective, I encountered 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 implemented a 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 indicated in log main.js 21:11 points to "xmlreader.read". It seems that the XML file being retrieved from the cloud cannot be processed by XMLReader.
Here is a snippet of 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>
The error mentions 'charAt'. I am relatively new to coding, having started just a month ago and reviewing W3 XML tutorials earlier today. Any assistance would be greatly appreciated!