Converting HTML input data to pre-format XML data

Discussion in 'Ruby' started by Elijah Odumosu, Apr 29, 2008.

  1. Hi All,

    I am really new to Ruby and here is my dilemma. I need to write a
    ruby-cgi (.rb) script that just spits back pre-formatted XML. Basically,
    my script would accomplish the following:

    a.) Read in your form variables (name-value pair).
    b.) Construct the right xml block.
    c.) Submit the XML to BaseCamp API.

    I have an interface to implement step c. However, as a start, I
    implemented "step a" (as shown below). It simply reads in my HTML-input
    fields (via submit from HTML-form to the script) and POST it to the
    server as "raw" text.

    #!c:\ruby\bin\ruby
    require "cgi"
    require 'rexml/document'
    include REXML

    cgi = CGI.new
    response = String.new

    #loop thru each name-value paired-element and save them in response
    cgi.params.each_pair{|key, value|
    response += "name = #{key}, value = #{value}<br/>"
    }


    cgi.out("charset" => "utf-8",
    "type" => "text/html") {
    response
    }


    produces:

    name = location5, value = Code for Location 5
    name = ScoutArea, value = Add New Resource
    name = FriendlyResponse, value = 9
    name = ForceNewBug, value = 9
    name = ScoutDefaultMessage, value = Thank you for submitting your issue.
    The request has been logged in the Issue Tracking System and will be
    handled in priority order.
    name = resourceURL, value = http://stuff.com
    name = Extra, value = This is a test
    name = location1, value = Code for Location 1
    name = location2, value = Code for Location 2
    name = resourceAnnotation, value = This is a test
    name = location3, value = Code for Location 3
    name = resourceType, value = Type
    name = location4, value = Code for Location 4
    name = ScoutProject, value = Project Name
    name = ScoutUserName, value = John Doe
    name = resourceTitle, value = New drugs for Type II Diabetes Patients
    name = Description, value = Add New Resource: New drugs for Type II
    Diabetes Patients


    "Step b" is to convert the "posted" text to a pre-formated XML block
    that will be submitted to BaseCamp API (step c).


    When I changed cgi-out to xml type shown below (with the rest of the
    script the same)

    cgi.out("charset" => "utf-8",
    "type" => "text/xml") {
    response

    it produces the following error message:

    XML Parsing Error: syntax error
    Location: http://mystuff/cgi-bin/Submitter.rb
    Line Number 1, Column 1:

    name = location5, value = Code for Location 5<br/>name = ScoutArea,
    value = Add New Resource<br/>name = FriendlyResponse, value = 1<br/>name
    = ForceNewBug, value = 1<br/>name = ScoutDefaultMessage, value = Thank
    you for submitting your issue. The request has been logged in the Issue
    Tracking System and will be handled in priority order.<br/>name =
    resourceURL, value = http://stuff.com<br/>name = Extra, value =
    ^

    QUESTION: I went through Ruby Standard Library and found some REXML
    class-method, but I am not familiar with which one to use and how to use
    them, yet.
    --
    Posted via http://www.ruby-forum.com/.
    Elijah Odumosu, Apr 29, 2008
    #1
    1. Advertising

  2. Elijah Odumosu

    Roger Pack Guest

    google for rexml, and maybe look around for some XML posts that look
    right and try and recreate them [?]

    On Tue, Apr 29, 2008 at 11:31 AM, Elijah Odumosu <> wrote:
    > Hi All,
    >
    > I am really new to Ruby and here is my dilemma. I need to write a
    > ruby-cgi (.rb) script that just spits back pre-formatted XML. Basically,
    > my script would accomplish the following:
    >
    > a.) Read in your form variables (name-value pair).
    > b.) Construct the right xml block.
    > c.) Submit the XML to BaseCamp API.
    >
    > I have an interface to implement step c. However, as a start, I
    > implemented "step a" (as shown below). It simply reads in my HTML-input
    > fields (via submit from HTML-form to the script) and POST it to the
    > server as "raw" text.
    >
    > #!c:\ruby\bin\ruby
    > require "cgi"
    > require 'rexml/document'
    > include REXML
    >
    > cgi = CGI.new
    > response = String.new
    >
    > #loop thru each name-value paired-element and save them in response
    > cgi.params.each_pair{|key, value|
    > response += "name = #{key}, value = #{value}<br/>"
    > }
    >
    >
    > cgi.out("charset" => "utf-8",
    > "type" => "text/html") {
    > response
    > }
    >
    >
    > produces:
    >
    > name = location5, value = Code for Location 5
    > name = ScoutArea, value = Add New Resource
    > name = FriendlyResponse, value = 9
    > name = ForceNewBug, value = 9
    > name = ScoutDefaultMessage, value = Thank you for submitting your issue.
    > The request has been logged in the Issue Tracking System and will be
    > handled in priority order.
    > name = resourceURL, value = http://stuff.com
    > name = Extra, value = This is a test
    > name = location1, value = Code for Location 1
    > name = location2, value = Code for Location 2
    > name = resourceAnnotation, value = This is a test
    > name = location3, value = Code for Location 3
    > name = resourceType, value = Type
    > name = location4, value = Code for Location 4
    > name = ScoutProject, value = Project Name
    > name = ScoutUserName, value = John Doe
    > name = resourceTitle, value = New drugs for Type II Diabetes Patients
    > name = Description, value = Add New Resource: New drugs for Type II
    > Diabetes Patients
    >
    >
    > "Step b" is to convert the "posted" text to a pre-formated XML block
    > that will be submitted to BaseCamp API (step c).
    >
    >
    > When I changed cgi-out to xml type shown below (with the rest of the
    > script the same)
    >
    > cgi.out("charset" => "utf-8",
    > "type" => "text/xml") {
    > response
    >
    > it produces the following error message:
    >
    > XML Parsing Error: syntax error
    > Location: http://mystuff/cgi-bin/Submitter.rb
    > Line Number 1, Column 1:
    >
    > name = location5, value = Code for Location 5<br/>name = ScoutArea,
    > value = Add New Resource<br/>name = FriendlyResponse, value = 1<br/>name
    > = ForceNewBug, value = 1<br/>name = ScoutDefaultMessage, value = Thank
    > you for submitting your issue. The request has been logged in the Issue
    > Tracking System and will be handled in priority order.<br/>name =
    > resourceURL, value = http://stuff.com<br/>name = Extra, value =
    > ^
    >
    > QUESTION: I went through Ruby Standard Library and found some REXML
    > class-method, but I am not familiar with which one to use and how to use
    > them, yet.
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    >
    Roger Pack, Apr 29, 2008
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rocky Moore
    Replies:
    7
    Views:
    1,720
    mikeb
    Jan 14, 2004
  2. =?Utf-8?B?S2V2aW4gQnVydG9u?=

    Pre-Send Request Headers, Pre-Send Request Content

    =?Utf-8?B?S2V2aW4gQnVydG9u?=, Dec 31, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    1,031
    =?Utf-8?B?S2V2aW4gQnVydG9u?=
    Dec 31, 2004
  3. Wladimir Borsov
    Replies:
    7
    Views:
    464
    Raymond Loman
    May 5, 2004
  4. Juha Nieminen
    Replies:
    3
    Views:
    1,132
    Juha Nieminen
    Feb 22, 2008
  5. Chris M. Thomasson
    Replies:
    10
    Views:
    639
    Nobody
    Aug 29, 2009
Loading...

Share This Page