【问题标题】:How do I create a dynamic web page that will list all XML files in a directory?如何创建将列出目录中所有 XML 文件的动态网页?
【发布时间】:2015-07-04 23:20:46
【问题描述】:

我在一个目录中有大约 200 个 XML 文件,这些文件会定期更改。目标是有一个“主页”,它从每个 XML 文件中获取一个“标题”元素,然后创建一个动态目录——我不想每次文件都手动编辑一个新的 HTML“主页”。更改、删除或添加。

有什么建议吗?尝试寻找答案已被证明是徒劳的。

【问题讨论】:

    标签: xml html xslt dom


    【解决方案1】:

    您需要某种服务器端方法来获取文件列表。一种可能的候选方法(您可以使用 XSLT 进行转换)是您的 HTTP 服务器显示目录列表的默认方法(您需要允许访问该文件的目录列表,并且您可以指定默认目录列表中的某种 XSLT 以将其转换为您在主页中想要的样子)。但是,这感觉有点笨拙,并且可能与您想要的其他安全策略不兼容。

    如果您可以使用其他一些服务器端技术(php、Ruby、Python、Perl、ASP.NET 等),那么可能有一种更清晰/更易于维护的方法。但是使用您列出的技术是不可能的(至少没有来自您的 httpd 的一点帮助)。

    【讨论】:

      【解决方案2】:

      回复很晚,但希望以后来的人可以利用这个。

      我认为您可以使用 node.js 轻松做到这一点。这是一个 sn-p,它将遍历您传递的目录并将其加载到数组中

      var fs = require('fs'); // import file system
      var path = require('path'); //import path, used to resolve file path
      
      var dir = <MAIN_DIR_PATH>; // DIr path were you want to search the xml file
      var xmlFile = []; //Array in which xml file will be stored
      logFileSystem(dir);
      
      // Recursively checks for xml 
      function logFileSystem(dirPath) {
          //Read the firectory
          fs.readdir(dirPath, function(err, files) {
              if (err) {
                  console.log("Could not read dir", err);
                  return;
              }
              //Loop through each file
              files.forEach(function(file, index) {
                  var filePath = path.join(dirPath, file);
                  fs.stat(filePath, function(err, stat) {
                      if (err) {
                          console.log("Error stating file", err);
                      }
                      if (stat.isFile()) {
                          if (filePath.endsWith("xml")) {
                              console.log("This is xml file = ",filePath)
                              xmlFile.push(filePath)
                          }
      
      
                      }
                      // If directory call the api again recursively
                      if (stat.isDirectory()) {
                          logFileSystem(filePath);
                      }
                  })
              })
          })
      
      }
      

      您现在可以通过 api 调用将此返回给用户。用node创建一个api真的很简单。

      希望这篇文章能帮助某人完成工作。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-04-16
        • 1970-01-01
        • 2011-03-13
        • 2014-03-08
        • 1970-01-01
        相关资源
        最近更新 更多