New to Ruby and trying to do a find and replace in the
content of an XML file with Ruby and Nokogiri.
Below is an example of the XML.
<?xml version="1.0" encoding="ISO-8859-1"?> Type: POWERFORGED Piston-IndividualHigh comp.Powerforged0.9805" Pin Dia.2 Ring(s)5/64 Groove1 Ring(s)3/16 Groove1.735 CD.142 dish; DUROSHIELD® skirt coated piston; .060" Oversize8WL-2323F 60 Type: POWERFORGED Piston-IndividualHigh comp.Powerforged0.9805" Pin Dia.2 Ring(s)5/64 Groove1 Ring(s)3/16 Groove1.735 CD.142 dish; DUROSHIELD® skirt coated piston; .060" Oversize8WL-2323F 60 Type: POWERFORGED Piston-IndividualHigh comp.Powerforged0.9805" Pin Dia.2 Ring(s)5/64 Groove1 Ring(s)3/16 Groove1.735 CD.142 dish; DUROSHIELD® skirt coated piston; Standard Size8WL-2323FThe code below Just changes the 1st node instead of iterating through
each node and doing the find and replace.
require ‘rubygems’
require ‘nokogiri’
file_name = ‘Aces.xml’
@doc = Nokogiri::XML(File.open(file_name))
partName = @doc.at_css (“Part”)
partName.content = partName.content.gsub!(/L-/,‘L’)# removes L-
File.write(“ACESFixed.xml”, @doc.to_xml)
Thank you
Bob