satya
Joined: 08 May 2008 Posts: 78
|
Posted: Thu Feb 25, 2010 12:17 pm Post subject: Generate XML file using Hpricot Gem |
|
|
Here is the sample application for generating xml using Hpricot gem
:>gem install hpricot
Generate sample application (run the below command)
:> rails hpricot
After that generate a controller (run the below command)
:> ruby script/generate controller xml
Now Place the below code in controller
DETAILS = [
{ :id =>1, :name => "EMP1", :exp => "3", :platform => "ROR" },
{ :id =>2, :name => "EMP2", :exp => "2.5", :platform => "Dot Net" },
{ :id =>3, :name => "EMP3", :exp => "2", :platform => "PHP" },
{:id =>5, :name => "EMP4", :exp => "2", :platform => "Designer"}
]
def index
end
def generate_with_rexml
hijack_response( generate_rexml )
end
def upload
uploaded_file = params[:xml_file]
data = uploaded_file.read if uploaded_file.respond_to? :read
if request.post? and data
case params[:commit]
when "Parse with Hpricot" : parse_with_hpricot( data )
end
else
redirect_to :action => 'index'
end
end
private
def hijack_response( out_data )
send_data( out_data, :type => "text/xml", :filename => "sample.xml" )
end
def generate_rexml
doc = REXML::Document.new
root = doc.add_element( "Details" )
DETAILS.each{ |element_data|
employe_element = root.add_element( "Employe" )
employe_element.add_attribute( "id", element_data[:id] )
name_element = employe_element.add_element( "Name" )
name_element.add_text( element_data[:name] )
exp_element = employe_element.add_element( "Exp" )
exp_element.add_text( element_data[:exp] )
platform_element = employe_element.add_element( "Platform" )
platform_element.add_text( element_data[:platform] )
}
doc.write( out_string = "", 2 )
return out_string
end
def parse_with_hpricot( xml_data )
doc = Hpricot.XML( xml_data )
(doc/:Name).each{ |name_element|
if name_element.inner_html == "EMP4"
parent = name_element.parent
parent.attributes["id"] = "4"
end
}
(doc/:Exp).each{ |exp_element|
if exp_element.inner_html == "2"
exp_element.inner_html = "2.5"
end
}
hijack_response( doc.to_s )
end
---Remove the comment from environment.rb file for the below line(Because database in no need for this application that's way remove the comment)
config.frameworks -= [ :active_record, :active_resource, :action_mailer ]
Now create index.rhtml file and place it in views/xml folder
Place the below code in index.rhtml
<%=link_to "Generate Using XML", :action => "generate_with_rexml" %>
<br />
Note: From the above link you can generate sample xml file
Here you can upload the Sample XML document that we generated above and click on Hpricot button you can generate the xml file with Help of Hpricot Gem
<% form_tag( {:action => 'upload'}, {:multipart => true} ) do -%>
<p>
File: <%= file_field_tag "xml_file" %><br/>
<%= submit_tag "Parse with Hpricot " %>
</p>
<% end -%>
Small search functionality is implemented (Find the Name and replace the id as ‘4’ in place of previous id and replace the exp as ‘2.5’ in place of previous exp)
Now run the below command in your command prompt
-----: ruby script/server
Thanks |
|