fairyliquid
Programmer
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
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