Ruby/XML/XML Builder

Материал из Wiki.crossplatform.ru

Перейти к: навигация, поиск

Содержание

Builder::XmlMarkup.new( :target => address, :indent => 1 )

#!/usr/bin/env ruby
require "rubygems"
require "builder"
address = <<XML
<address>
 <name><given>M</given><family>M</family></name>
 <street>St.</street>
 <city>G</city>
 <state>C</state>
 <code>81</code>
 <country>USA</country>
</address>
XML
document = Builder::XmlMarkup.new( :target => address, :indent => 1 )
puts document.to_xs



Create an XML document from a here document

#!/usr/bin/env ruby
require "rexml/document"
include REXML
address = <<XML
<address>
 <name><given>A</given><family>B</family></name>
 <street>9876 St.</street>
 <city>CA</city>
 <state>Colorado</state>
 <code>81000</code>
 <country>USA</country>
</address>
XML
document = Document.new( address )
puts document



Use builder to create an XML document from hash

#!/usr/bin/env ruby
 
require "rubygems"
require "builder"
favorites = {
  "candy" => "Neccos", 
  "novel" => "Empire", 
  "holiday" => "Easter"
}
xml = Builder::XmlMarkup.new( :target => $stdout, :indent => 2 )
xml.instruct! :xml, :version => "1.0", :encoding => "US-ASCII"
xml.favorites do
 favorites.each do | name, choice |
  xml.favorite( choice, :item => name )
 end
end



XmlMarkup

#!/usr/bin/env ruby
require "rubygems"
require "builder"
include Builder
document = XmlMarkup.new(:target => $stdout, :indent => 1)
document.instruct!
document.rument! "our good friend Mondo"
document.name( :lang => "en" ) {
  document.given "A", :nickname => "false"
  document.family "B"
}