Break from a loop which is run on a xml

how do i loop through a xml and break when i find the node with a
specific value…really struggling to break out of a xml loop…most of
the iterators just loop but cannot be break - ed

please help me with this

for e.g.

this is d code where i find the files in a dir & check d earlier time
stamp for each file which is saved in a xml & do a comparison for each

Find.find(’./’){|path|
if path != ‘./abcd.rb’
if ! File::directory? ( path )
modtime = File.mtime(path)
xmldoc.elements.each(“course/file”){ |element|
oldtime = Time.parse(element.attributes[“modified”])
if modtime > oldtime
$changed = “yes”
break
else
$changed = “no”
end
}
newfile = course.add_element ‘file’, {‘version’=>‘2.0’,
‘modified’=> modtime, ‘download’=>$changed}
newfile.text = path[2,path.length]
end
end
}

this is the sample xml…

xyzw.pdf content/banner/t1-st_1.swf content/t1/st_1/page1.swf content/t1/st_2/page1.swf content/t1/st_2/page2.swf content/t1/st_2/page3.swf

hi Mythpills,

try to reduce your code to an executable snippet and then post that -
what you’ve posted above cannot be executed as there are undefined
variables.

  • j

On Thu, Dec 1, 2011 at 7:02 AM, mythpills P. [email protected]
wrote:

how do i loop through a xml and break when i find the node with a
specific value…really struggling to break out of a xml loop…most of
the iterators just loop but cannot be break - ed

Use a proper XPath expression. Other than that I second what Jake said.

Kind regards

robert

sorry guys…its company code cannot make it public.neways i figured out
a way out by using arrays and running a for loop over it…works just the
way i wanted

wish there was a obvious way to do things in Ruby…its concise but it is
cumbersome

On Wed, Dec 7, 2011 at 7:59 AM, mythpills P. [email protected]
wrote:

sorry guys…its company code cannot make it public.

Then strip it down / change it to a simple case which shows the
problem you are trying to solve.
http://sscce.org/

neways i figured out
a way out by using arrays and running a for loop over it…works just the
way i wanted

Chances are that using doc#at_xpath with Nokogiri is simpler than what
you built.

wish there was a obvious way to do things in Ruby…its concise but it is
cumbersome

I think there is an obvious way (using XPath to select exactly the
nodes you want) but without you showing a proper example we cannot
show it to you. I find it a tad unfair to blame Ruby when you are not
providing proper information.

Kind regards

robert

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs