Forum: Ruby Newbie question Modifying an XML file

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
F20e2a9ce2cb679f09f4fd6920847268?d=identicon&s=25 Bob Biehl (shortythepimp)
on 2016-11-11 21:58
New to Ruby and trying to do a find and replace in the <Part></Part>
content of an XML file with Ruby and Nokogiri.

Below is an example of the XML.
<?xml version="1.0" encoding="ISO-8859-1"?>
<ACES version="3.0">
<App action="A" id="123432" validate="yes"><BaseVehicle
id="16548"/><EngineBase id="5460"/><Note>Type: POWERFORGED
Piston-Individual</Note><Note>High
comp.</Note><Note>Powerforged</Note><Note>0.9805" Pin Dia.</Note><Note>2
Ring(s)</Note><Note>5/64 Groove</Note><Note>1 Ring(s)</Note><Note>3/16
Groove</Note><Note>1.735 CD</Note><Note>.142 dish; DUROSHIELD® skirt
coated piston; .060" Oversize</Note><Qty>8</Qty><PartType
id="5620"/><Part>WL-2323F 60</Part></App>
<App action="A" id="123433" validate="yes"><BaseVehicle
id="16548"/><EngineBase id="1397"/><Note>Type: POWERFORGED
Piston-Individual</Note><Note>High
comp.</Note><Note>Powerforged</Note><Note>0.9805" Pin Dia.</Note><Note>2
Ring(s)</Note><Note>5/64 Groove</Note><Note>1 Ring(s)</Note><Note>3/16
Groove</Note><Note>1.735 CD</Note><Note>.142 dish; DUROSHIELD® skirt
coated piston; .060" Oversize</Note><Qty>8</Qty><PartType
id="5620"/><Part>WL-2323F 60</Part></App>
<App action="A" id="123434" validate="yes"><BaseVehicle
id="16548"/><EngineBase id="5460"/><Note>Type: POWERFORGED
Piston-Individual</Note><Note>High
comp.</Note><Note>Powerforged</Note><Note>0.9805" Pin Dia.</Note><Note>2
Ring(s)</Note><Note>5/64 Groove</Note><Note>1 Ring(s)</Note><Note>3/16
Groove</Note><Note>1.735 CD</Note><Note>.142 dish; DUROSHIELD® skirt
coated piston; Standard Size</Note><Qty>8</Qty><PartType
id="5620"/><Part>WL-2323F</Part></App>
</ACES>


The 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
C2d1339038b5fec16b5b15878f648d96?d=identicon&s=25 Jesus Castello (jesus_castello)
on 2016-11-11 23:18
Hey Bob,

you need to use the 'css' method, instead of the 'at_css' method. The
difference is that 'at_css' only returns the first match.

In addition, you also need to iterate over the result set after you
change your code to use 'css'.

Example:

################
parts = doc.css('Part')

parts.each do |part|
  part.content = part.content.gsub!(/L-/,'L')
end

File.write("ACESFixed.xml", @doc.to_xml)
################

Hope that helps :)
- Jesus Castello, http://blackbytes.info
F20e2a9ce2cb679f09f4fd6920847268?d=identicon&s=25 Bob Biehl (shortythepimp)
on 2016-11-12 00:26
Perfect.
Thank you.
F20e2a9ce2cb679f09f4fd6920847268?d=identicon&s=25 Bob Biehl (shortythepimp)
on 2016-11-12 00:36
Is there a way to nest the gsubs?
This topic is locked and can not be replied to.