Yanni Mac
2009-Jun-29 18:33 UTC
Replacing XML tag with a string based on parameters in tag?
I am trying to figure out a (better) way to do the following: some_string = "Display a widget here - <widget>1234</widget>" new_string = replace_widgets_in_string(some_string) p new_string => #Display a widget here - Widget 1234 is blue" the method replace_widgets_in_string will need to pick out the xml tag and value (which will be the model id) and from there I will find the model and return a string that replaces the widget tags. Is there an existing library or easy way to do this that I might not be thinking of? I am assuming rexml wont work, since its not a well formed doc. I want to avoid coding a monolithic method using string splits. Any ideas? -- Posted via http://www.ruby-forum.com/.
Philip Hallstrom
2009-Jun-29 19:19 UTC
Re: Replacing XML tag with a string based on parameters in tag?
On Jun 29, 2009, at 11:33 AM, Yanni Mac wrote:> > I am trying to figure out a (better) way to do the following: > > some_string = "Display a widget here - <widget>1234</widget>" > new_string = replace_widgets_in_string(some_string) > p new_string => #Display a widget here - Widget 1234 is blue" > > the method replace_widgets_in_string will need to pick out the xml tag > and value (which will be the model id) and from there I will find the > model and return a string that replaces the widget tags. > > Is there an existing library or easy way to do this that I might not > be > thinking of? I am assuming rexml wont work, since its not a well > formed > doc. I want to avoid coding a monolithic method using string splits. > Any ideas?Well, if you''re string will only ever contain one instance of the widget this would work... >> some_string =~ /<(.*?)>(\d+)<\/\1>/ => 24 >> $1 => "widget" >> $2 => "1234" Otherwise I''d look into hpricot/nokigirl and let it parse it. -philip