tags:

views:

145

answers:

3

I have a simple XML string, that is more or less always the same. I'd rather avoid using an XML parser for such a little piece of code, and I though Regexp would help.

The XML string looks like :

<?xml version="1.0"?>
<methodCall>
  <methodName>weblogUpdates.extendedPing</methodName>
  <params>
    <param>
      <value>Official Google Blog</value>
    </param>
    <param>
      <value>http://googleblog.blogspot.com/&lt;/value&gt;
    </param>
    <param>
      <value>http://googleblog.blogspot.com/&lt;/value&gt;
    </param>
    <param>
      <value>http://googleblog.blogspot.com/atom.xml&lt;/value&gt;
    </param>
  </params>
</methodCall>

I want to extract the values of each param (and maintain the order).

I came up with /<value>(.*)<\/value>/xi but that just macthes the very first value :/

+1  A: 

Parsing XML with Ruby is trivial, please don't try to parse XML with a regular expression - it is notoriously difficult and error-prone.

While it may be tempting to try and use a regular expression, please don't. No matter how hard you try to smash that nail with the screwdriver it won't work as a hammer - please use one of the many wonderful hammers at your disposal.

Andrew Hare
Please, stop spreading this meme: parsing XML with a regular expression is not "notoriously difficult and error-prone", it is simply impossible. And not impossible in the sense of "impossible to get right", but provably mathematically impossible. In fact, pretty much every CS student on the entire planet will at one time during his/her career have proven this impossibility in some homework assignment or other.
Jörg W Mittag
@Jörg - I am afraid that you and I are talking about two very different things. Mathematical impossibility is very different from real impossibility (for lack of a better phrase). Is it impossible to use regular expressions on XML? No, of course not - it is entirely possible to use regular expressions to hack up a solution that will work most of the time. I understand your point (and agree with you in principle) but it really doesn't have much bearing on a practical discussion like this one.
Andrew Hare
+1  A: 

Normally should use XML parser, but I still think its a bit overkill.

If me I will do like this.

x = File.new("test.xml", "r").read
puts x.scan(/<value>(.*)<\/value>/)

Results

Official Google Blog
http://googleblog.blogspot.com/
http://googleblog.blogspot.com/
http://googleblog.blogspot.com/atom.xml

if you like to loop something for each values, you can do like this

x.scan(/<value>(.*)<\/value>/) do |x|
  puts x
end
S.Mark
Perfect... exactly what I needed. Parsing XML is an overkill in that specific case. Thx!
Julien Genestoux
A: 

As just a side comment, for this specific application it may feel difficult but learning nokogiri or libxml may help you make a decision about more complex xml parsing down the line. Besides, parsing xml in ruby really is pretty trivial nowadays and doing it The Right Way will at least make it easy to expand to a non-trivial method when your client eventually requests that you do something ridiculously out of scope which involves full xml parsing :)

For other frameworks and technology I probably wouldn't recommend such investment, but nokogiri is painless. And if you just feel like playing you could try out Hpricot and get your dose of _why for the day (RIP).

Chuck Vose