Created
September 13, 2012 04:17
-
-
Save xianhuazhou/3711814 to your computer and use it in GitHub Desktop.
xml validation with nokogiri
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'nokogiri' | |
require 'find' | |
class XMLValidate | |
class << self | |
def run path | |
files = 0 | |
failed_files = 0 | |
Find.find path do |file| | |
next unless file =~ /\.xml$/ | |
failed_files += 1 unless xml_validate file | |
files += 1 | |
end | |
puts "#{files} validated, #{failed_files} failed." | |
puts "Done." | |
end | |
def xml_validate file | |
doc = Nokogiri::XML.parse( | |
File.read(file), | |
nil, | |
nil, | |
Nokogiri::XML::ParseOptions::DTDVALID | |
) | |
true | |
rescue Nokogiri::XML::SyntaxError => e | |
puts "#{file}: #{e}" | |
false | |
end | |
end | |
end | |
path = ARGV[0] | |
unless path and File.directory?(path) | |
puts "Usage: ruby #{__FILE__} /path/to/xml/directory" | |
exit 1 | |
end | |
XMLValidate.run path |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment