Created
October 3, 2013 11:12
-
-
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.
This file contains hidden or 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
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