问题描述
我的目标是获取一个 xml 文件的元素,该文件已保存在 Parse Cloud 上,用于我的 Express 应用程序.我在为 xmlreader 引用 this 后开始编码以及 Parse httpRequest 检索解析文件的指南.我像这样使用 xmlreader 测试读取 xml 元素:
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).
然后我尝试使用 httprequest 从解析云中获取 xml 文件,它也有效.
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)
我使用了云函数,这是我的 main.js
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);
});
});
});
});
});
注意:log main.js 21:11 中显示的错误指向xmlreader.read".所以很明显从云端获取的xml文件是xmlreader无法浏览的.
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.
我的 xml 文件:
<?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>
注意错误charAt".我一个月前开始编码,今天中午看了 w3 xml tuts.请帮忙!
Note error 'charAt'.I started coding a month before and looked at w3 xml tuts today noon. Please help!
推荐答案
已解决.首先,XML 文件中有一个错字.将PID"替换为ParentId".但是与问题相关的错误是..就像@timothy 建议的那样,httpRequest 方法的结果位于thefile"中,而文件位于thefile.buffer"中.对于 xmlreader 输入使用:thefile.buffer.toString().main.js 中的循环函数也有错误,因为我放错了 .at().我将最终代码附加到 从 Parse Cloud 检索 xml 文件并使用 xmlreader 获取其节点元素
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);
});
});
});
});
和xml文件是
<?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>
以及日志结果:
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.
这篇关于无法在 Express 应用程序中使用 xmlreader 从 Parse Cloud 中的 xml 文件解析数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!