Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace XML node contents using Nokogiri

I'm using Ruby to read an XML document and update a single node, if it exists, with a new value.

http://www.nokogiri.org/tutorials/modifying_an_html_xml_document.html is not obvious to me how to change the node data, let alone how to save it back to the file.

def ammend_parent_xml(folder, target_file, new_file)
  # open parent XML file that contains file reference
  get_xml_files = Dir.glob("#{@target_folder}/#{folder}/*.xml").sort.select {|f| !File.directory? f}
  get_xml_files.each { |xml|

    f       = File.open(xml)

    # Use Nokgiri to read the file into an XML object
    doc     = Nokogiri::XML(f)
    filename  = doc.xpath('//Route//To//Node//FileName')

    filename.each_with_index {
      |fl, i|
      if target_file == fl.text
        # we found the file, now rename it to new_file
        # ???????
      end

    }

  }

end

This is some example XML:

<?xml version="1.0" encoding="utf-8">
    <my_id>123</my_id>
    <Route>
      <To>
        <Node>
          <Filename>file1.txt</Filename>
          <Filename>file2.mp3</Filename>
          <Filename>file3.doc</Filename>
          <Filename>file4.php</Filename>
          <Filename>file5.jpg</Filename>
        </Node>
      </To>
    </Route>
</xml>

I want to change "file3.doc" to "file3_new.html".

I would call:

def ammend_parent_xml("folder_location", "file3.doc", "file3_new.html")
like image 597
Beertastic Avatar asked Feb 20 '15 10:02

Beertastic


1 Answers

To change an element in the XML:

@doc = Nokogiri::XML::DocumentFragment.parse <<-EOXML
<body>
  <h1>OLD_CONTENT</h1>
  <div>blah</div>
</body>
EOXML


h1 = @doc.at_xpath "body/h1"
h1.content = "NEW_CONTENT"

puts @doc.to_xml   #h1 will be NEW_CONTENT

To save the XML:

file = File.new("xml_file.xml", "wb")
file.write(@doc)
file.close

There's a few things wrong with your sample XML.

  • There are two root elements my_id and Route
  • There is a missing ? in the first tag
  • Do you need the last line </xml>?

After fixing the sample I was able to get the element by using the example by Phrogz:

element = @doc.xpath("Route//To//Node//Filename[.='#{target_file}']").first 

Note .first since it will return a NodeSet.

Then I would update the content with:

element.content = "foobar"
like image 118
jmccure Avatar answered Oct 15 '22 11:10

jmccure