# Adding new xml element with hpricot

**URL:** <https://www.ruby-forum.com/t/adding-new-xml-element-with-hpricot/162973>\
**Category:** Ruby\
**Created:** [March 16, 2009, 11:21am UTC](https://www.ruby-forum.com/t/adding-new-xml-element-with-hpricot/162973 "2009-03-16T11:21:08Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![milo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85e7bf/32.png) [@milo](https://www.ruby-forum.com/u/milo)\
**Post date:** [March 16, 2009, 11:21am UTC](https://www.ruby-forum.com/t/adding-new-xml-element-with-hpricot/162973/1 "2009-03-16T11:21:08Z")

</div>

I’ve got a lot of xml files that look something like this:

some text some text an integer ... an integer ... more stuff

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.
