Forum: Ruby Adding new xml element with hpricot

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Milo T. (Guest)
on 2009-03-16 12:21
I've got a lot of xml files that look something like this:

<preferences>
<pref_1>some text</pref_1>
<pref_2>some text</pref_2>
<pref_3>an integer</pref_3>
...
<pref_of_interest>an integer</pref_of_interest>
...
<pref_n>more stuff</pref_n>
</preferences>

There could be no "pref_of_interest" fields or there could be several.
What I need to do is the following:

1. If there are no pref_of_interest fields, or there's already a
pref_of_interest set to the desired value, leave the file untouched.
2. If there are prefs_of_interest and none contain the required value,
add a new pref_of_interest element with the required value in.

I have not had much success with hpricot past parsing the file. Can
anyone suggest the proper way to add the new element? Thanks.
This topic is locked and can not be replied to.