在for循环中进行xml解析以回调

时间:2014-04-20 05:21:30

标签: xml node.js

所以我有一个node.js函数,它读取6个xml文件并查找特定的搜索项并将其返回给用户..

...
var xmlresult = '<?xml version="1.0" encoding="utf-8"?><rss xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" xml:lang="en_EN" version="2.0"><channel>';
    for ( var i = 1; i <= 6; i++) {
        filename = i + '.xml';
        fs.readFile(filename, 'utf8', function(err, data) {
            if (err) {
                return console.log(err);
            }
            var doc = libxml.parseXmlString(data);
            var xpath = "/rss/channel/item[contains(translate(description, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'),'"+ userquery + "')]";
            console.log(xpath);
            var result = doc.find(xpath);
            console.log(result);
            result.forEach(function(item) {
                xmlresult += item;
                // console.log(item.text());
            });
        });
    }
    xmlresult += "</channel></rss>";
    // console.log(result[0].text());
    respond(xmlresult);

    function respond(data) {
        // response.set('Content-Type','text/xml').send(data);
        response.write(data);
        response.end();
    }
...

现在的问题是,甚至在xml解析发生之前就会发生响应。我知道,因为节点是异步的。现在如果我尝试通过向for循环添加语句来解决问题

if(i==6){
xmlresult += "</channel></rss>";

respond(xmlresult);
}

由于fs和libxmljs异步,它仍然无法解决问题。我也试过了。那我该怎么办?

1 个答案:

答案 0 :(得分:0)

没关系。使用async.eachSeries

解决
var xmlresult = '<?xml version="1.0" encoding="utf-8"?><rss xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" xml:lang="en_EN" version="2.0"><channel>';
var filenames = [ '1.xml', '2.xml', '3.xml', '4.xml', '5.xml', '6.xml' ];
i = 0;
// filenames.forEach(function(filenames){
async.eachSeries(filenames, processfile, respond);

// });
function processfile(filename,callback) {
    fs
            .readFile(
                    filename,
                    'utf8',
                    function(err, data) {
                        if (err) {
                            return console.log(err);
                        }
                        var doc = libxml.parseXmlString(data);
                        var xpath = "/rss/channel/item[contains(translate(concat(title,description), 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'),'"
                                + userquery + "')]";
                        console.log(xpath);
                        var result = doc.find(xpath);
                        console.log(result);
                        result.forEach(function(item) {
                            xmlresult += item;
                            // console.log(item.text());
                        });
                        callback();
                    });

}

function respond(err) {
    if(err!=null){
        console.log(err);
    }
    xmlresult += "</channel></rss>";
    console.log("sending back");
    //respond(xmlresult);
    // response.set('Content-Type','text/xml').send(data);
    response.write(xmlresult);
    response.end();
}

}