2014-09-28 148 views
0

所以我寫這Node.js的程序導入XML文件轉換成JSON對象的數組。我有2個文件導入,teachers.xml和students.xml。node.js中等待任務完成

教師和學生包含幾千有關教師和學生信息的。我用我的代碼覆蓋了這部分。

這是我的JavaScript文件解析文件:

var fs = require('fs'); 
var xmldom = require('xmldom'); 
var async = require('async'); 

// Parse `catalog` xml file and call `callback(catalog domxml root)` 
function parse_catalog(catalog, callback) { 
    // Read xml file content 
    fs.readFile(catalog, function (err, data) { 
     if (err) { 
      throw 'Error reading XML catalog'; 
     } else { 
      // Parse xml content and return dom root 
      var domRoot = new xmldom.DOMParser().parseFromString(data.toString()); 
      // Call callback passing dom root 
      callback(domRoot) 
    } 
}); 
} 

我有兩個方法,這樣對XML轉換成JSON和它的工作完美(一個教師,一個用於學生)

// Convert teacher XML into json format in a array 
function convert_teachers(domCatalog) { 
    var teachers = domCatalog.getElementsByTagName('teacher'); 
    var teachers_arr= []; 
    for (var i = 0; i < teachers .length; i++) { 
     var teacher= teachers[i]; 
     ... 
     //Reading the xml 

     teachers_arr.push({ 
     ... 
     //Create the json 

     }); 
    } 
    console.log("Teachers are now in JSON format "); 
} 

所以最終我必須做的是這樣的:

parse_catalog('teachers.xml', convert_teachers); 

當我這樣做:

parse_catalog('teachers.xml', convert_teachers); 
parse_catalog('students.xml', convert_students); 

一方或另一方將完成第一個因要素進口的數量,女巫是正常的,我認爲。

我要的是等待兩個需要進口,然後做一些JavaScript操作,這是我在哪裏卡住了。

我試着用異步做到這一點,但它不沒有等到這兩個文件完成導入。

async.parallel([ 
    function(callback) { 
     parse_catalog('teachers.xml', convert_teachers); 
     callback(); 
    }, 

    function(callback) { 
     parse_catalog('students.xml', convert_students); 
     callback(); 
    } 
], function(err) { 
    if (err) return next(err); 

    console.log("Finished"); 
    //Enventually some Javascript manipulations on the two arrays 

}); 

其實它輸出:

Finished 
Teachers are now in JSON format 
Students are now in JSON format 

,或者根據文件的大小

Finished 
Students are now in JSON format 
Teachers are now in JSON format 

我想更重要的是這樣的:

Teachers are now in JSON format (or students) 
Students are now in JSON format (or teachers) 
Finished 

我打算加載2個以上的文件,以及它們的順序加載對我無關緊要。

任何線索?謝謝!

回答

1

您在async.parallel()函數中執行callback()的功能太快,因爲那時fs.readFile()還沒有開始。嘗試這樣的事情,而不是:

function parse_catalog(catalog, callback) { 
    // Read xml file content 
    fs.readFile(catalog, function(err, data) { 
    if (err) 
     return callback(err); 

    // Parse xml content and return dom root 
    var domRoot = new xmldom.DOMParser().parseFromString(data.toString()); 

    // Call callback passing dom root 
    callback(null, domRoot); 
    }); 
} 

// Convert teacher XML into json format in a array 
function convert_teachers(domCatalog) { 
    var teachers = domCatalog.getElementsByTagName('teacher'); 
    var teachers_arr = []; 
    for (var i = 0; i < teachers .length; i++) { 
    var teacher = teachers[i]; 
    ... 
    //Reading the xml 

    teachers_arr.push({ 
    ... 
    //Create the json 

    }); 
    } 
    console.log('Teachers are now in JSON format'); 
    return teachers_arr; 
} 
// and similarly for `convert_students` 

async.parallel({ 
    teachers: function(callback) { 
    parse_catalog('teachers.xml', function(err, domCatalog) { 
     if (err) 
     return callback(err); 
     var teachers = convert_teachers(domCatalog); 
     callback(null, teachers); 
    }); 
    }, 
    students: function(callback) { 
    parse_catalog('students.xml', function(err, domCatalog) { 
     if (err) 
     return callback(err); 
     var students = convert_students(domCatalog); 
     callback(null, students); 
    }); 
    } 
}, function(err, results) { 
    if (err) return next(err); 

    console.log('Finished'); 

    // here you have `results.teachers` and `results.students` 
    console.dir(results); 
}); 
+0

謝謝!你能指出爲什麼在parse_catalog中添加「callback(null,domRoot)」嗎? – metraon 2014-09-30 02:52:09

+1

因爲'parse_catalog()'正在執行一個異步操作,所以你需要在調用結果時調用回調。如果導致錯誤,那麼錯誤對象將作爲第一個參數傳遞(否則爲'null',表示成功)。如果沒有錯誤,則將結果('domRoot')傳遞給回調函數。 – mscdex 2014-09-30 03:07:26