gem install builder
require 'builder'
x = Builder::XmlMarkup.new(:target =>
$stdout, :indent => 1)
#":target =>$stdout"参数:指示输出内
容将被写向标准输出控制台
#":indent =>1"参数:XML输出形式将被缩
进一个空格字符x.instruct! :xml,
:version =>'1.1',:encoding => 'gb2312'
x.comment! "书本信息"
x.library("shelf" => "Recent Acquisitions") {
x.section("name" => "ruby"){
x.book("isbn" => "0672310001"){
x.title "Programming Ruby"
x.author "Yukihiro "
x.description "Programming Ruby -
The Pragmatic Programmer's Guide"
}
}
}
< ?xml version="1.1" encoding="gb2312"?> < !-- 书本信息 --> < library shelf="Recent Acquisitions"> < section name="ruby"> < book isbn="0672310001"> < title>Programming Ruby< /title> < author>Yukihiro < /author> < description>Programming Ruby - The Pragmatic Programmer's Guide < /description> < /book> < /section> < /library> < inspect/> #< IO:0x2a06ae8>
<library shelf="Recent Acquisitions">
<section name="Ruby">
<book isbn="0672328844">
<title>The Ruby Way</title>
<author>Hal Fulton</author>
<description>
Second edition. The book you are now reading.
Ain't recursion grand?
</description>
</book>
</section>
<section name="Space">
<book isbn="0684835509">
<title>The Case for Mars</title>
<author>Robert Zubrin</author>
<description>Pushing toward a second home for the human
race.
</description>
</book>
<book isbn="074325631X">
<title>First Man: The Life of Neil A. Armstrong</title>
<author>James R. Hansen</author>
<description>Definitive biography of the first man on
the moon.
</description>
</book>
</section>
</library>
require 'rexml/document'
include REXML
input = File.new("books.xml")
doc = Document.new(input)
root = doc.root
puts root.attributes["shelf"] # Recent Acquisitions
doc.elements.each("library/section") { |e| puts e.attributes["name"] }
# Output:
# Ruby
# Space
doc.elements.each("*/section/book") { |e| puts e.attributes["isbn"] }
# Output:
# 0672328844
# 0321445619
# 0684835509
# 074325631X
sec2 = root.elements[2]
author = sec2.elements[1].elements["author"].text # Robert Zubrin
require 'rexml/document'
require 'rexml/streamlistener'
include REXML
class MyListener
include REXML::StreamListener
def tag_start(*args)
puts "tag_start: #{args.map {|x| x.inspect}.join(', ')}"
end
def text(data)
return if data =~ /^\w*$/ # whitespace only
abbrev = data[0..40] + (data.length > 40 ? "..." : "")
puts " text : #{abbrev.inspect}"
end
end
list = MyListener.new
source = File.new "books.xml"
Document.parse_stream(source, list)
tag_start: "library", {"shelf"=>"Recent Acquisitions"}
tag_start: "section", {"name"=>"Ruby"}
tag_start: "book", {"isbn"=>"0672328844"}
tag_start: "title", {}
text : "The Ruby Way"
.........................................
book1 = XPath.first(doc, "//book") # Info for first book found
p book1
# Print out all titles
XPath.each(doc, "//title") { |e| puts e.text }
# Get an array of all of the "author" elements in the document.
names = XPath.match(doc, "//author").map {|x| x.text }
p names
<book isbn='0672328844'> ... </> The Ruby Way The Case for Mars First Man: The Life of Neil A. Armstrong ["Hal Fulton", "Robert Zubrin", "James R. Hansen"]
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有