#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2009
    Posts
    2
    Rep Power
    0

    Question Ruby - First program Need Help !


    Hello Everyone,

    I wrote my first ruby program n i dont understand whats wrong here..

    can you pl tell? I want to convert my xml file into 1 string and view it.
    I get error as
    parseXml.rb:7:in `read_file_as_a_string': undefined method `+' for nil:NilClass (NoMethodError)
    from parseXml.rb:15

    ================================
    require 'rexml/document'

    def read_file_as_a_string(filename)
    resulstr = " "
    File.open(filename) do |infile|
    while (line = infile.gets)
    resultstr = resultstr + line
    puts resultstr
    end
    end
    file.close
    return resultstr
    end

    # get xml data as a string
    xml_data = read_file_as_a_string("sample.xml")
    puts xml_data
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2009
    Posts
    2
    Rep Power
    0
    Sorry, the progam i wrote is below...
    ===================================
    require 'rexml/document'
    def read_file_as_a_string(filename)
    resulstr = " "
    file = File.new(filename,'r')
    while (line = file.gets)
    resultstr = resultstr + line
    puts resultstr
    end
    file.close
    return resultstr
    end

    # get xml data as a string
    xml_data = read_file_as_a_string("sampleAlarm.xml")
    puts xml_data
    ==========================
    error i get is
    parseXml.rb:7:in `read_file_as_a_string': undefined method `+' for nil:NilClass (NoMethodError)
    from parseXml.rb:15
    ==========================
    Ruby version - ruby 1.8.5 (2006-12-25 patchlevel 12) [i686-linux]
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2009
    Posts
    4
    Rep Power
    0
    I don't have ruby installed on this machine, but have you tried something like this?

    Code:
    require 'rexml/document'
    include REXML
    
    def read_file_as_a_string(filename)
      doc = Document.new(File.new(filename)) 	
      puts doc
    end
    
    # get xml data as a string
    read_file_as_a_string("sampleAlarm.xml")
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2009
    Posts
    3
    Rep Power
    0
    Couldn't tell if you still needed help.

    A quick script - may possibly work. (It would have been nice to see samples of the xml file and what you want to extract from it.)

    For example, you want to extract node "XYZ".

    Code:
    # Script XYZ.txt
    var str input
    cat "sample.xml" > $input
    stex -r -c "^<XYZ&\>^]" $input > null
    stex -r -c "[^</XYZ&\>^" $input > null
    echo $input

    The documentation for the stex (string extractor) is at http://www.biterscripting.com/helppages/stex.html .

    Call the script with the script call

    Code:
    script "XYZ.txt"

IMN logo majestic logo threadwatch logo seochat tools logo