0

I am having trouble getting xmlSimple to run correctly in my .rb file. I am using xmlSimple.xmlIn(filename);, however, it seems there is an error with finding the correct file. I have moved the file to the bin and the file exists, meaning, filename.exists? = true. Any ideas on the possible error source? Thanks!

-Edit- Let me add this information; I am very new to Ruby and there is a good chance my method or syntax is completely wrong, here is my code in the .rb file:

require 'xmlsimple'

file_name = 'xmldatatest.xml'

paragraph_str = 0

file = File.open(file_name) # takes XML Data and creates a file of the data

File.open(file_name, "w+") do |f|     # open file for update

lines = f.readlines           # read into array of lines

lines.each do 

    |it|           
                  # modify lines
        it.gsub!(/\n/, '')
    it.gsub!('<p>', '')
    it.gsub!('</p>', '')
    it.gsub!('\"Paragraph.\"', 'Paragraph')
    if ((it.include? ('Paragraph')) == 1)
    paragraph_str += 1
    end

   while paragraph_str > 0 do

    initial_value = paragraph_str

    if ((paragraph_str == initial_value))
    it.gsub!(/Paragraph/, '<p>')
    paragraph_str -= 1
    else 
    it.gsub!(/Paragraph/, '</p><p>')
    paragraph_str -= 1
    end

    end    
    f.print lines                 # write out modified lines
    end
end

File.open(file_name, 'a') {|f| f.puts "</p>" }

ref  = XmlSimple.xml_in(file_name)

The purpose of the program is to strip all escape characters from the original XML file and then replace each "Paragraph#" node within a <p> and </p> tag. After which, the file would be parsed using XmlSimple.Xml_in(filename). Any suggestions or corrections are more than appreciated.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Anthony
  • 1
  • 1
  • Please supply some sample XML. It is very likely you do not need to preprocess the XML to make it readable, but we can't tell without seeing what you are dealing with. [Nokogiri](http://nokogiri.org) is a highly regarded XML parser for Ruby, and it's easy to learn to use. I'd recommend looking into learning it. – the Tin Man Jul 04 '11 at 10:08

2 Answers2

0

It is my understanding that, in ruby, xml is dead slow unless bound to a C library. And that nokogiri is the best maintained of the lot.

Adding to this, re your question:

however, it seems there is an error with finding the correct file

If you're using threads and checking if a file exists, it may return true|false in the if statement but the opposite might be true by the time it's actually read.

Adding further to this the usual yada yada about parsing html using regex applies/

Community
  • 1
  • 1
Denis de Bernardy
  • 75,850
  • 13
  • 131
  • 154
0

Some things I noticed:

file = File.open(file_name) # takes XML Data and creates a file of the data

File.open(file_name, "w+") do |f|     # open file for update

lines = f.readlines           # read into array of lines

Note that you are attempting to read from the file handle f, which is actually opened for writing. f.readlines should probably be file.readlines.

A more Ruby-like way of dealing with it is:

lines = []
File.open(file_name) do |f|
  lines = f.readlines
end

Another issue that would bother me in a code review, is you are opening the same file for reading that you are for appending. I would recommend that you read from one, and write to another, newly created file, then after you are finished, close both, rename the old and then rename the new file to the name of the old file, then delete the renamed old file if you don't want it any more. This is a protective tactic in case something fails during processing.

it.gsub!(/\n/, '') could be written as it.chomp!.

if ((it.include? ('Paragraph')) == 1) could be if (it['Paragraph']).

the Tin Man
  • 158,662
  • 42
  • 215
  • 303