INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

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.

Jobs

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

(OP)
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:

<appSettings>
    <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="\\127.0.0.1\fs0\SomePlace\boris.txt" />
    <add key="TestAssetXsl" value="D:\Program Files\EmailTemplates\Failed.xsl" />
</appSettings>


xmldoc2 looks like this:

<?xml version="1.0" encoding="utf-8" ?>
<Configuration>
<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>
</Configuration>



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

OR

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']
 end

 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
 end

I'd be grateful if anyone could help me?

Thanks in advance

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

Hi
  • 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
end

Feherke.
http://free.rootshell.be/~feherke/

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!

Resources

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