ruby on rails-如何使用Nokogiri在XML中用一些值替换标记



我有一个预定义的XML模板,其中包含一些需要替换的标记。标记值动态地来自前端。

<?xml version="1.0"?>
<catalog>
   <book id="bk101">
      <author>AUTHOR1</author>
      <title>TITLE1</title>
      <genre>Computer</genre>
      <price>44.95</price>
      <publish_date>2000-10-01</publish_date>
      <description>An in-depth look at creating applications 
      with XML.</description>
   </book>
   <book id="bk102">
      <author>AUTHOR2</author>
      <title>TITLE2</title>
      <genre>Fantasy</genre>
      <price>5.95</price>
      <publish_date>2000-12-16</publish_date>
      <description>A former architect battles corporate zombies, 
      an evil sorceress, and her own childhood to become queen 
      of the world.</description>
   </book>
 </catalog>

在上面的例子中,我需要动态地用实际值替换TITLE1TITLE2AUTHOR1AUTHOR2

最好的方法是什么?我在一些Ruby代码中使用了Nokogiri,但运气不好。

基本思想是需要在XML中搜索<book>标记。对于找到的每本书,检索应用于它的值块。找到<author>标记并替换其文本。查找<title>标记,并替换其文本。然后转到下一本书。

然而,在您的示例中,当一个简单的gsub将一次性完成时,编写代码来完成这项工作是过分的:

xml = '<?xml version="1.0"?>
<catalog>
   <book id="bk101">
      <author>AUTHOR1</author>
      <title>TITLE1</title>
      <genre>Computer</genre>
      <price>44.95</price>
      <publish_date>2000-10-01</publish_date>
      <description>An in-depth look at creating applications 
      with XML.</description>
   </book>
   <book id="bk102">
      <author>AUTHOR2</author>
      <title>TITLE2</title>
      <genre>Fantasy</genre>
      <price>5.95</price>
      <publish_date>2000-12-16</publish_date>
      <description>A former architect battles corporate zombies, 
      an evil sorceress, and her own childhood to become queen 
      of the world.</description>
   </book>
 </catalog>
'
values = {
  'TITLE1' => 'Moby Dick',
  'AUTHOR1' => 'Herman Melville',
  'TITLE2' => 'Tom Sawyer',
  'AUTHOR2' => 'Mark Twain',
}
puts xml.gsub(Regexp.union(values.keys), values)
# >> <?xml version="1.0"?>
# >> <catalog>
# >>    <book id="bk101">
# >>       <author>Herman Melville</author>
# >>       <title>Moby Dick</title>
# >>       <genre>Computer</genre>
# >>       <price>44.95</price>
# >>       <publish_date>2000-10-01</publish_date>
# >>       <description>An in-depth look at creating applications 
# >>       with XML.</description>
# >>    </book>
# >>    <book id="bk102">
# >>       <author>Mark Twain</author>
# >>       <title>Tom Sawyer</title>
# >>       <genre>Fantasy</genre>
# >>       <price>5.95</price>
# >>       <publish_date>2000-12-16</publish_date>
# >>       <description>A former architect battles corporate zombies, 
# >>       an evil sorceress, and her own childhood to become queen 
# >>       of the world.</description>
# >>    </book>
# >>  </catalog>

gsub的这种用法并不经常使用,但在将值代入模板时,我已经使用过很多次了。使用在文档中保证唯一的标记/键是必不可少的,所以我经常使用前导和尾随双下划线来标记它们。换句话说,__TITLE1____AUTHOR1__

这样可以很容易地替换其他字段的内容,如<genre><price>等。

以与键/标记相同的形式命名变量,任务变得更加容易,因为您应该收到字段名称和字段值的哈希,这将成为gsub中使用的哈希的来源。

在替换之前,请确保对值进行验证/消毒。用户键入错误和恶意的用户可能会故意输入数据,试图破坏您的代码,或者更糟的是,破坏XML输入的内容。

相关内容

  • 没有找到相关文章

最新更新