Showing posts with label XML. Show all posts
Showing posts with label XML. Show all posts

Monday, July 23, 2012

Generate Rss feed with rails application

  Hey guys,
      Lets today we create the rss feed with our existing rails application. Consider we have controller called post and have index method to show all the available posts. Now meanwhile we need to create feed for available posts as well.

controller:

def index
  @posts = Post.find(:all, :order => "created_at desc")
  respond_to do |format|
     format.html { render :template => 'posts/index.rhtml' }
     format.xml { render :template => 'posts/index.rxml', :layout => false
        headers["Content-Type"] = "application/rss+xml"
     }
  end
end

view:

index.rxml

xml.instruct!
xml.rss "version" => "2.0", "xmlns:dc" => "http://purl.org/dc/elements/1.1/" do
  xml.channel do
    xml.title 'Available Posts'
    xml.description h("Here is the posts ... blah blah")
   
    @posts.each do |post|
      xml.item do
        xml.title post.title
        xml.link url_for(:only_path => false,
          :controller => 'posts',
          :action => 'show',
          :fishery => post.id)
        xml.description post.description
        xml.pubDate CGI.rfc1123_date(post.created_at)
      end
    end
  end
end

index.rhtml

# Add auto discovery tag to access rss feed

<%= auto_discovery_link_tag :rss, {:controller => "posts", :action => "index"}%>

See your rss feed for posts is ready!! Whenever auto discovery tag found it enables the rss icon on browser. Keep in mind Either you have to use predefined xml node or need to create XSL template. You can also emend the stylesheet into XSL template.

Wednesday, November 30, 2011

Handle JSON or XML request and response

Hello Guys,
        Some time we required to pass the request in particular format and get response back in same format. 

1. Consider the request and response format as JSON

Request:
curl -H "Accept: application/json" -i -X GET http://DOMAIN:PORT/controller/action/parameters
eg. curl -H "Accept: application/json" -i -X GET http://127.0.0.1:3000/posts/show/1

Response:
In the show method of posts controller we have to check whether request type is JSON than return response back to JSON format.

def show
  if request.format == Mime::JSON 
     post = Post.find_by_id(params[:id])
     render :json => post.to_json, :status => 200
  end
end

Now we get response as the requested post in .json. we will parse the json by JSON::load(response)

2. Consider the request and response format as XML

Request:
curl -H "Accept: application/xml" -i -X GET http://DOMAIN:PORT/controller/action/parameters
eg. curl -H "Accept: application/xml" -i -X GET http://127.0.0.1:3000/posts/show/1

Response:
In the show method of posts controller we have to check whether request type is XML than return response back to XML format

def show
  if request.format == Mime::XML
     post = Post.find_by_id(params[:id])
     render :xml => post, :status => 200
  end
end

Now we get response as the requested post in .xml. we will parse the xml using Hpricot or any other parser.

Hope this post will help you to deal with json and xml format.

Monday, November 21, 2011

Anemone - web crawler

Hello Guys,
         Anemone is a free, multi-threaded ruby web spider framework. It is useful for collecting information about websites. It's crawl sites with initial level. With Anemone you can write task to generate statistics on a site just by giving it the URL. Anemone supports the nokogiri for HTML and XML parsing.

Lets see the simple example.. so you can get the idea how it works
 
First of all we have to install the anemone gem by
gem install anemone

It will install anemone along with dependencies robots, nokogiri.

require 'anemone'

desc "crawl the website data at initial level"
task :crawl_website => :environment do
  Anemone.crawl("http://priyankapathak.wordpress.com/") do |anemone|
    anemone.on_every_page do |page|
      puts page.url
      # store the visited pages in file system or db
    end
  end
end

As an above example, that will take a domain as 'http://priyankapathak.wordpress.com', and start tracing every page. If you want to store traced pages than just write the code to store at db or file 
system. Invoke above task by rake crawl_website --trace

There are many other inbuilt methods available with anemone. like
  • after_crawl - run a block on the PageHash (a data-structure of all the crawled pages) after the crawl is finished
  • focus_crawl - use a block to select which links to follow on each page
  • on_every_page - run a block on each page as they are encountered
  • on_pages_like - given one or more RegEx patterns, run a block on every page with a matching URL
  • skip_links_like - given one or more RegEx patterns, skip the any link that matches patten
If you find this ruby web spider interesting and want more information then simply follow below links.

Thursday, August 18, 2011

Validate XSD with XML

If you want to validate the XSD (XML schema) with XML document then here is the easy steps.



First you need to install libxml-ruby via gem install command.



We have 2 files. one is example.xsd and example.xml. Lets we define normal method say validate_xml_schema.

require 'libxml'
def validate_xml_schema
   begin
      schema = XML::Schema.document(XML::Document.file("#{RAILS_ROOT}/example.xsd"))
      xml_instance = XML::Document.file("#{RAILS_ROOT}/example.xml")                  
      if xml_instance.validate_schema(schema)
         puts "Successfully validate schema"
      else
         puts "Oops!! There is some problem in XML formatting"
      end     
   rescue => e
     puts "Exception:" + e.inspect
   end 
end

Here is the two example documents.

example.xml

<?xml version="1.0" encoding="UTF-8"?>
<students>
   <student>
      <roll_no>1001</roll_no>
      <name>Priyanka Pathak</name>
      <mark subject='biology'>48</mark>
      <mark subject='physics'>42</mark>
   </student>
   <student>
      <roll_no>1002</roll_no>
      <name>Rahul Pathak</name>
      <mark subject='biology'>45</mark>     
      <mark subject='physics'>43</mark>
   </student>
</students>


example.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
  <xs:element name="students">
    <xs:complexType>
      <xs:sequence>
        <xs:element maxOccurs="unbounded" ref="student"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:element name="student">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="roll_no"/>
        <xs:element ref="name"/>
        <xs:element minOccurs="1" maxOccurs="2" ref="mark"/>    
      </xs:sequence>
    </xs:complexType>
  </xs:element>

  <xs:element name="roll_no" type="xs:integer"/>

  <xs:element name="name" type="xs:string"/>

  <xs:element name="mark">
    <xs:complexType>
      <xs:simpleContent>
          <xs:extension base="xs:integer">
            <xs:attribute name="subject" use="required">
              <xs:simpleType>
                <xs:restriction base="xs:string">          
                  <xs:enumeration value="biology"/>
                  <xs:enumeration value="physics"/>
                </xs:restriction>
              </xs:simpleType>
            </xs:attribute>
          </xs:extension>
      </xs:simpleContent>
    </xs:complexType>
  </xs:element>
</xs:schema>


XSD schema defines structure of nodes and value type.
eg. roll_no must be positive integer.
      name must be string.
      mark must contain subject as attribute with option like 'biology' or 'physics' and value type as decimal.

Hope this post helps you.