简体   繁体   English

解析目录中的所有xml文件

[英]Parse all xml files in a directory

I'm looking for a way to parse a bunch of xml files in the following directory structure: 我正在寻找一种解析以下目录结构中的xml文件的方法:

db/
  -> dir1/
       -> file.xml
  -> dir2/
       -> file.xml
  ...

All xml files have the same name but under different directories. 所有xml文件都具有相同的名称,但位于不同的目录下。

Prior to this I had a single xml file that I was parsing using ajax like so: 在此之前,我有一个使用ajax解析的xml文件,如下所示:

  $(document).ready(function() { $.ajax({ type: "GET", url: "db/file.xml", dataType: "xml", success: function (xml) { // parsing done here } }); }); 

Is there a similar way to do the above but recursively? 是否有类似的方法可以递归进行上述操作? Thanks in advance! 提前致谢!

I might have been too early to post, but I ended up using xml2js: 我可能还为时过早,但最终还是使用了xml2js:

const path = require('path')
const glob = require('glob');
const fs = require('fs');
const xml2js = require('xml2js');

$(document).ready(function() {
    var files = glob.sync(path.join(__dirname, '../db/*/*.xml'));
    var counter = 0;
    files.forEach(function(f) {
        fs.readFile(f, function(err, data) {
            var parser = new xml2js.Parser({explicitArray : false});
            parser.parseString(data, function(err, xml) {
                // parsing done here
            });
            counter++;
            if (counter === files.length) {
                $(document).trigger('completed-xml-db-parse');
            }
        });
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM