Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Find and replace xml from xmldoc to another using Nokogiri

Status
Not open for further replies.

fairyliquid

Programmer
Sep 9, 2010
7
GB
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
 
Hi
[ul]
[li][tt]xpath[/tt] returns a [tt]NodeSet[/tt], use [tt]at_xpath[/tt] to get a single [tt]Node[/tt][/li]
[li]there is no ElementContent property ( note that in Ruby identifiers starting with uppercase letter denote constants ( and classes, which are special constants ), use [tt]content[/tt][/li]
[/ul]
Code:
config_dictionary[teal].[/teal]each [b]do[/b] [teal]|[/teal]key[teal],[/teal] value[teal]|[/teal]
  node [teal]=[/teal] xmldoc[teal].[/teal]at_xpath[teal]([/teal][green][i]'//'[/i][/green] [teal]+[/teal] key [teal])[/teal]
  [b]next[/b] [b]if[/b] node[teal]==[/teal][b]nil[/b] [gray]# do not crash if not found[/gray]
  node[teal].[/teal]content [teal]=[/teal] value
[b]end[/b]

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top