Skip to content

Instantly share code, notes, and snippets.

@snt
Created October 3, 2013 11:12
Show Gist options
  • Save snt/6808138 to your computer and use it in GitHub Desktop.
Save snt/6808138 to your computer and use it in GitHub Desktop.
REXML parsing example. Parse and format recursively under the directory specified as first arg.
require 'rexml/document'
def showxml(fn)
doc = REXML::Document.new(open(fn))
doc.elements.each("//.") { |e|
x=e
ps = []
while x != nil do
ps << x.name
x = x.parent
end
path = ps.reverse.inject { |es, e| es + "/" + e }
id = e.attributes["id"]
if path then
puts " " + path + ((e.text != nil and e.text.strip() != "") ? (" => " + e.text) : "")
end
puts " [" + id + "]" if id
e.attributes.collect.reject{ |k,v|
k.start_with?("xmi:") or k.start_with?("xmlns:")
}.each { |k,v|
puts " @" + k + " = " + v
}
puts
}
end
rootdir = ARGV[0]
Dir["#{rootdir}/**/*.xml"].each { |x|
puts x + " :"
showxml(x)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment