rubyhacker / gmail.com wrote:
> Question for you all. I want to treat HTML like XML
> (which is no big deal).
>
> But I want to find certain "special" tags (not real
> HTML) and replace them with my own text.
>
> It's macro-type stuff. Basically I want to output
> the *same* HTML except for the text that replaced
> the special tags.
>
> I can't find any examples of generating XML with
> REXML. It should be easy, I don't want it to be
> too hard.
>
> Contrived example below in case it helps.
>
> How would you do this?
>
> Thanks,
> Hal
>
>
> Input:
>
> <html>
> <body>
> <p>Hi, there.</p>
> <foo bar="this" bam="that">some more text</foo>
> <p>That's all.</p>
> </body>
> </html>
>
> Output:
> <html>
> <body>
> <p>Hi, there.</p>
> <p>I found a foo tag enclosing 'some more text' with
> bar and bam values of 'this' and 'that'...</p>
> <p>That's all.</p>
> </body>
> </html>

require 'xml-split.rb'

tag = 'foo'
DATA.read.xml_split(tag).each {|stuff|
  if stuff.class == String
    print stuff
  else
    attr = stuff[0].xml_parse
    puts "<p>I found a #{tag} tag enclosing '#{stuff[1]}' with"
    print "#{attr.keys.join(' and ')} values of "
    print "'#{attr.values.join("' and '")}'...</p>"
  end
}

__END__
<html>
<body>
<p>Hi, there.</p>
<foo bar="this" bam="that">some more text</foo>
<p>That's all.</p>
</body>
</html>

---- output ----

<html>
<body>
<p>Hi, there.</p>
<p>I found a foo tag enclosing 'some more text' with
bam and bar values of 'that' and 'this'...</p>
<p>That's all.</p>
</body>
</html>