正则表达式去除所有XML

时间:2016-03-12 23:52:39

标签: ruby

我是Ruby的新手,我需要编写ruby正则表达式去除所有XML并创建一个带有titles而不是XML的文件:

例如,第一本书应该是:

书:bk101

作者:Mathew Gamardella(先注意名字!!!)

标题:XML开发人员指南

类型:电脑

价格:44.95

发布日期:2000年10月1日(请注意,这与XML不同 - 您必须将日期转换为此表单)

描述:深入了解创建应用程序       使用XML

这是我的XML文件 -

<?xml version="1.0"?>
<catalog>
   <book id="bk101">
      <author>Gambardella, Matthew</author>
      <title>XML Developer's Guide</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>Ralls, Kim</author>
      <title>Midnight Rain</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>
   <book id="bk103">
     <author>Corets, Eva</author>
     <title>Maeve Ascendant</title>
     <genre>Fantasy</genre>
     <price>5.95</price>
     <publish_date>2000-11-17</publish_date>
     <description>After the collapse of a nanotechnology 
     society in England, the young survivors lay the 
     foundation for a new society.</description>
   </book>
   <book id="bk104">
      <author>Corets, Eva</author>
     <title>Oberon's Legacy</title>
     <genre>Fantasy</genre>
     <price>5.95</price>
     <publish_date>2001-03-10</publish_date>
     <description>In post-apocalypse England, the mysterious 
     agent known only as Oberon helps to create a new life 
      for the inhabitants of London. Sequel to Maeve 
     Ascendant.</description>
  </book>
  <book id="bk105">
      <author>Corets, Eva</author>
      <title>The Sundered Grail</title>
     <genre>Fantasy</genre>
     <price>5.95</price>
     <publish_date>2001-09-10</publish_date>
     <description>The two daughters of Maeve, half-sisters, 
     battle one another for control of England. Sequel to 
     Oberon's Legacy.</description>

    </catalog>

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

这看起来更像是家庭作业而不是问题。我会让你弄清楚如何编写文件和格式化日期 - 这里有一些简单的东西,可以用你的XML制作哈希并一次一个地遍历每本书/字段(我把你的文档简化为两本书)

require 'active_support/core_ext/hash'

xml_books = <<-EOF 
"<?xml version="1.0"?>
<catalog>
   <book id="bk101">
      <author>Gambardella, Matthew</author>
      <title>XML Developer's Guide</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>Ralls, Kim</author>
      <title>Midnight Rain</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>
EOF

books = Hash.from_xml(xml_books)
books['catalog']['book'].each do |e|
  e.keys.each do |k|
    printf("%s -> %s\n", k, e[k])
  end # do k
end # do e

产生以下输出:

id -> bk101
author -> Gambardella, Matthew
title -> XML Developer's Guide
genre -> Computer
price -> 44.95
publish_date -> 2000-10-01
description -> An in-depth look at creating applications
      with XML.
id -> bk102
author -> Ralls, Kim
title -> Midnight Rain
genre -> Fantasy
price -> 5.95
publish_date -> 2000-12-16
description -> A former architect battles corporate zombies,
     an evil sorceress, and her own childhood to become queen
     of the world.