检查格式正确的 xml 并将错误记录到文件

checking well formed xml and logging the error to file

我在文件夹 a、b、c 和 d 中有 4,000 个 xml 个文件 每个文件夹各包含 1000 个文件。所有文件夹都在名为 library 的主文件夹中 我需要使用

检查 xml 文件是否格式正确

xmllint --noout 100.xml"

命令或者可能有更好的东西。 现在,如果出现错误,请在日志文件中记录文件名和文件夹名。

log "library/a/100.xml"

下面是伪代码。我需要在 shell 脚本或更快的

中将脚本构建为 运行
#program check xml format
#!/bin/bash
echo Please, get ready to process
 for i in $(cat "/home/thrinity/library/);
  do
    xmllint --noout "$i" ;
    if error
      #log filefolder & file name
      print error to errorlog.txt
    else
end do

我正在寻找标签丢失的错误..类似.. 038339 此处发票结束标签丢失或我可以捕获的任何方式

对于那些可能感兴趣的人。下面的代码在 Ubuntu 14.04 机器

中对我有用

find /YourMainFolder -name '*.xml' -print | xargs -I "{}" sh -c 'File="{}";xmllint --noout "${File}" || readlink -f {} >> errorlog.txt

find /YourMainFolder -name '*.xml' -print | xargs -I "%ARG%" sh -c 'File="%ARG%";xmllint --noout "${File}" || echo "Error in ${File}" > errorlog.txt'

将取决于您的错误,此处基于 Return 代码 <> 0

使用缺少的标签

find /YourMainFolder -name '*.xml' -print | xargs -I "%ARG%" sh -c 'File="%ARG%";grep -q -E "<invoice>.*</invoice>" || echo "Error in ${File}" > errorlog.txt'