Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Find and replace xml from xmldoc to another using Nokogiri

Find and replace xml from xmldoc to another using Nokogiri

Find and replace xml from xmldoc to another using Nokogiri

I have 2 xml files.  I want to see if xmldoc1 has the same named elements in xmldoc2.  If it does, replace xmldoc2's element value with xmldoc1's.

xmldoc1 looks like this:

    <add key="TestAssetIngestXsl" value="D:\Program Files\SomePlace\AdvertAssetIngestSuccessful.xsl" />
    <add key="TestSubXsl" value="D:\Program Files\SomePlace\IdFailed.xsl" />
    <add key="TestIdSubXsl" value="D:\Program Files\SomePlace\Utils\frog.exe" />
    <add key="Something" value="\\\fs0\SomePlace\boris.txt" />
    <add key="TestAssetXsl" value="D:\Program Files\EmailTemplates\Failed.xsl" />

xmldoc2 looks like this:

<?xml version="1.0" encoding="utf-8" ?>
<TestAssetXsl>C:\Program Files\EmailTemplates\Failed.xsl</TestAssetXsl>
<TestAssetIngestXsl>C:\Program Files\EmailTemplates\AdvertAssetIngestSuccessful.xsl</TestAssetIngestXsl>
<TestSubXsl>C:\Program Files\Temp\AdvertCreativeUpsertFailed.xsl</TestSubXsl>
<TestIdSubXsl>C:\Program Files\Temp\AdvertProgIdFailed.xsl</TestIdSubXsl>

I have initially created from xmldoc1 a dictionary of all the key value pairs I wish to compare against the xmldoc2 file.

I was then attempting to spool through either the dictionary seeing if any elements in xmldoc2 shared the same name and change their values


Spin through each node in xmldoc2 and see if any key from the dictionary matches the elemnent name in xmldoc2 and then replace the element value.

I thought there might have been a node.content method that would enable me to set the new value for the node, but there isn't.

My code so far:

 xmldata = Nokogiri::XML File.open xmldoc1

 #get file data to update to
 xmldoc = Nokogiri::XML File.open xmldoc2

 config_dictionary = {}
 xmldata.xpath('//add').each do |node|    
 config_dictionary[node['key']] = node['value']

 config_dictionary.each do |key, value|
     node = xmldoc.xpath('//' + key )
     puts "\n Before Change:"
     puts node
     newnode = xmldoc.xpath('//' + key ).ElementContent = value
     puts "\n After Change:"
     puts newnode
     #found.content = value

I'd be grateful if anyone could help me?

Thanks in advance

RE: Find and replace xml from xmldoc to another using Nokogiri

  • xpath returns a NodeSet, use at_xpath to get a single Node
  • there is no ElementContent property ( note that in Ruby identifiers starting with uppercase letter denote constants ( and classes, which are special constants ), use content

CODE --> ( fragment )

config_dictionary.each do |key, value|
  node = xmldoc.at_xpath('//' + key )
  next if node==nil # do not crash if not found
  node.content = value


Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close