tags:

views:

73

answers:

2

i'm having a hard time translating this php code into rails.

$doc = new DOMDocument();
$doc->loadXML($in);

/* Iterating through the XML and store the data points into the $list array */
$params = $doc->getElementsByTagName( "param" );
foreach( $params as $param )
{
    $names = $param->getElementsByTagName( "name" );
    $name = $names->item(0)->nodeValue;

    $values = $param->getElementsByTagName( "value" );
    $value = $values->item(0)->nodeValue;

    $list[$name] = "'".mysql_escape_string($value)."'";
}
+1  A: 

This question doesn't have much to do with rails so much as Ruby + DOM. You might take a look here and in the Ruby and Rails documentation for XML parsers.

TerryP
+1  A: 

You may want to take a look at Nokogiri. An untested, wild guess of the code you need goes by the lines:

doc = Nokogiri::XML(in)
doc.xpath('//param').each do |param|
  name = param.search('//name').first.content
  value = param.search('//value').first.content
  list[name] = __escape_sql(value)
end

Where the function __escape_sql is left as an exercise to the reader...

Chubas