Skip to content

Instantly share code, notes, and snippets.

@xianhuazhou
Created September 13, 2012 04:17
Show Gist options
  • Save xianhuazhou/3711814 to your computer and use it in GitHub Desktop.
Save xianhuazhou/3711814 to your computer and use it in GitHub Desktop.
xml validation with nokogiri
#!/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