开发者

Can anybody translate this PHP code into Rails

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开发者_StackOverflow中文版->item(0)->nodeValue;

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

    $list[$name] = "'".mysql_escape_string($value)."'";
}


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.


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...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜