Contents |
This example is appropriate for small files only. Stream parsing is a necessity for larger XML files.
require "rexml/document"
file = File.new( ARGV[0] )
doc = REXML::Document.new file
doc.elements.each('html/body/a') do |e|
puts e.attributes["href"]
end
...
doc.elements.each('html/body/a') do |e|
e.attributes["href"] = "http://whatever.org/"
end
doc.write $stdout
Stream parsing must be used for larger XML files. This example outputs the 'href'-attribute of all 'a' tags.
require "rexml/document"
require "rexml/streamlistener"
class MyListener
include REXML::StreamListener
def tag_start(name, attrs)
case name
when "a"
puts attrs["href"]
end
end
end
file = File.new( ARGV[0] )
REXML::Document.parse_stream(file,MyListener.new)
This is example could be used as a template for a program that outputs a copy of the input stream, but with certain attributes changed.
class MyListener
include REXML::StreamListener
def tag_start(name, attrs)
case name
when "a"
...
else
end
e = REXML::Element.new(name)
attrs.each do |att, value|
e.attributes[att] = value
end
puts e.to_s.sub(/\/>/, '>')
end
def tag_end(name)
puts REXML::Element.new(name).to_s.sub(/<(.*)\/>/, '</\1>')
end
end
?