-1

I am very new with Ruby and I need to write the ruby regular expressions to strip off all the XML and create a file with titles instead of XML:

for example the first book should be:

book: bk101

author: Mathew Gamardella (notice first name first!!!)

title: XML Developer's Guide

Genre: Computer

Price: 44.95

Publish Date: October 1,2000 (Notice this is different from the XML - you must convert the date to this form)

Description: An in-depth look at creating applications with XML

Here is my XML file -

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

Any help is really appreciated.

MSITAR
  • 13
  • 3
  • 3
    You don't need regexps for this task. Check nokogiri gem and at least try to do it yourself. Come back when you have issues, show the code and ask a question. Stackoverflow is for helping developers, not a free "do it instead of me" resource and your question is more like: "here are requirements, get it done". – Rustam Gasanov Mar 13 '16 at 02:06
  • 1
    [Please don't try to parse XML with regex. Use an xml-parser instead](http://stackoverflow.com/a/1732454/3933332) – Tobi Nary Mar 14 '16 at 13:57
  • Thanks Rustam for lead. here is what i came up with using Nokogiri, however, it just give me an output for author with below code, require 'nokogiri' doc = Nokogiri::XML(File.open("test.xml")) doc.xpath('//author').each do |char_element| puts char_element.text end as soon as i add other notes in this, it do not give my any output at all require 'nokogiri' doc = Nokogiri::XML(File.open("test.xml")) doc.xpath('//author//title').each do |char_element| puts char_element.text – MSITAR Mar 15 '16 at 03:37
  • Why can't you use Nokogiri, or Ox, or Oga ...? – Tilo Mar 15 '16 at 04:11
  • Thanks Rustam for lead. here is what i came up with using Nokogiri, however, it just give me an output for author with below code, require 'nokogiri' doc = Nokogiri::XML(File.open("test.xml")) doc.xpath('//author').each do |char_element| puts char_element.text end as soon as i add other notes in this, it do not give my any output at all require 'nokogiri' doc = Nokogiri::XML(File.open("test.xml")) doc.xpath('//author//title').each do |char_element| puts char_element.text – MSITAR Mar 15 '16 at 17:23

1 Answers1

0

This looks more like a homework assignment than a question. I'll let you figure out how to write files and format the date --- here's something simple that will make a hash out of your XML and loop through each book / field one at a time (I shortened your document to two books).

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

Produces the following output:

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.
JLB
  • 323
  • 3
  • 8