Adding new xml element with hpricot

M

Milo Thurston

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top