xml - Substituting text in a file with Ruby -


i need read in file in xml format crammed single line, , need parse line find specific property , replace value have specified.

the file might contain:

<?xml version="1.0" encoding="utf-8" standalone="no"?><verificationpoint type="screenshot" version="2"><description/><verification object=":qp1b11_qlabel" type="png"> 

i need search through line, find property "verification object=" , replace :qp1b11 own string. please not don't want replace _qlabel" type="png"> part of string if possible.

i can't use sub don't value of property anything, , believe should able regular expressions have never had use them before , examples i've seen make me more confused earlier.

if can present me elegant answer (and explanation if using regexp) huge help!

thanks

you have xml use xml parser. nokogiri make short work of that:

doc = nokogiri::xml(that_string) doc.search('verification').each |node|     node['object'] = node['object'].sub(/:qp1b11/, 'pancakes') end new_string = doc.to_xml # <?xml version="1.0" encoding="utf-8" standalone="no"?>\n<verificationpoint type="screenshot" version="2">\n  <description/>\n  <verification object="pancakes_qlabel" type="png">\n</verification>\n</verificationpoint>\n" 

you can adjust output format using options to_xml.

if have 1 <verification> this:

node = doc.at('verification') node['object'] = node['object'].sub(/:qp1b11/, 'pancakes') new_string = doc.to_xml 

in either case you'd adjust regex , replacement suit needs.


Comments

Popular posts from this blog

objective c - Change font of selected text in UITextView -

php - Accessing POST data in Facebook cavas app -

c# - Getting control value when switching a view as part of a multiview -