Skip to content

Add and Remove elements from an existing document

koenhandekyn edited this page Apr 5, 2011 · 1 revision

to add tags to an existing document, use a Builder that works at a specific point in a document to remove tags from a document, find the tags and simply remove them.

the following snippet

    doc = Nokogiri::XML(communication)
    Nokogiri::XML::Builder.with(doc.at('root')) do |xml|
      xml.credit("value" => @cost)
    end
    doc.search('//document/data').each do |node|
      node.remove
    end
    doc.to_s

modifies this

<communication>
 <document><data>xxx</data></document>
</communication>

into

<communication>
 <document></document>
 <credit value="12"/>
</communication>