Sunday, June 2, 2013

[Rails] Need to understand the Nokogiri::XML::Builder Class

I was doing some coding to get to be familiarized with `Class
Nokogiri::XML::Builder`.

require 'nokogiri'

items = [[987,"Coupler",5],[654,"Connector",3],[579,"Clasp",1]]

builder = Nokogiri::XML::Builder.new(:encoding => 'UTF-8') do |xml|
xml.Order {
items.each do |i|
xml.Item{
xml.ItemId i.first
xml.ItemName i[1]
xml.Quantity i.last
}
end
}
end
puts builder.to_xml

Output:

<?xml version="1.0" encoding="UTF-8"?>
<Order>
<Item>
<ItemId>987</ItemId>
<ItemName>Coupler</ItemName>
<Quantity>5</Quantity>
</Item>
<Item>
<ItemId>654</ItemId>
<ItemName>Connector</ItemName>
<Quantity>3</Quantity>
</Item>
<Item>
<ItemId>579</ItemId>
<ItemName>Clasp</ItemName>
<Quantity>1</Quantity>
</Item>
</Order>

###############################################

Till now I am done. But really don't understand the difference between
`context` and `doc` attributes. Can anyone give me some simple examples
to understand these two?

--
Posted via http://www.ruby-forum.com/.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/9665c25787ea0e19a6cb02b47a8650fe%40ruby-forum.com?hl=en-US.
For more options, visit https://groups.google.com/groups/opt_out.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate