您的位置:首页 > 其它

操作XML文件

2011-11-15 13:15 288 查看
# 操作函数:

require 'rexml/document'
require 'pathname'
include REXML

filePath = File.join(Pathname.new(File.dirname(__FILE__)).realpath,'book.xml')
# 创建一个文件对象
input = File.new(filePath)
# 解析XML文件 input
doc = Document.new(input)

# 创建根节点
root =doc.root

#在根节点下创建元素:子节点 mytest <mytest><mytest/>
el = root.add_element("mytest")

#在mytest下面创建子节点元素 another 并且赋值id =10  <another id='10'>
#结果如下:<mytest> <another id='10'><mytest/>
el2 = el.add_element "another", {"id"=>"10"}

#创建一个元素节点 blah<>
el3 = Element.new "blah"
el.elements << el3
el3.attributes["myid"] = "sean"

#增加文本节点方法一
el4 = Element.new "xl"
el.elements << el4
el4.text= "sean"

#增加文本节点方法二
e15=Element.new "myelement"
#定义父节点
el.elements<<e15
e15.add_text"Hello dolly"

#增加文本节点方法三

#输出root内容中shelf的属性值  根元素节点

puts doc.elements["library/section[@name='Ruby']"].text
puts 0
root.attributes["shelf"]
puts doc
puts
#xpath 三个方法:first each match
puts  1
puts XPath.first(doc,"//book/")
puts
puts  2.1
XPath.each(doc,"//title") {|e| puts e.text}
puts  2.2
puts XPath.match(doc,"//title").map {|x| x.text}
puts
puts 3.1
XPath.match(doc,"library/section/book/author").map {|x| puts  x.text}
puts 3.2
doc.elements.each("library/section/book/author/") {|e| puts e.text}
puts 3.3
doc.elements.each("//author/") {|e| puts e.text}

#查看主体部分元素elements 1的值 elements是个数组
#REXML中的Element子节点的索引从1开始,而不是0。
#因为XPath就是从1开始进行计数的,REXML维持了这种关系
root.elements[1].to_s
)
#元素节点 attributes
puts
doc.elements.each("library/section/") {|e| puts e.attributes["name"]}

puts
doc.elements.each("library/section/book/"){|e| puts e.attributes["isbn"]}
#文本节点 text
puts
puts 7.1
doc.elements.each("library/section/book/title/"){|e| puts e}
puts 7.2
puts doc.elements.each("library/section/book/title/"){|e| e}
puts 7.3
puts XPath.each(doc,"//title") {|e| e.text}
puts
puts "读出 description"
doc.elements.each("library/section/book/description/") {|e| puts e.text}

#查看主体部分元素2的值
sec2 = root.elements[2]
#查看主体部分元素3的值
puts root.elements[3].to_s

book.xml文件内容:

<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>

操作结果:

<mytest><another id='10'/><blah myid='sean'/><xl>sean</xl><myelement>Hello dolly</myelement></mytest>

銆??銆??銆??銆?
0
<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>
<mytest><another id='10'/><blah myid='sean'/><xl>sean</xl><myelement>Hello dolly</myelement></mytest></library>

1
<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>

2.1
The Ruby Way
The Case for Mars
First Man: The Life of Neil A. Armstrong
2.2
The Ruby Way
The Case for Mars
First Man: The Life of Neil A. Armstrong

3.1
Hal Fulton
Robert Zubrin
James R. Hansen
3.2
Hal Fulton
Robert Zubrin
James R. Hansen
3.3
Hal Fulton
Robert Zubrin
James R. Hansen

Ruby
Space

0672328844
0684835509
074325631X

7.1
<title>The Ruby Way</title>
<title>The Case for Mars</title>
<title>First Man: The Life of Neil A. Armstrong</title>
7.2
<title>The Ruby Way</title>
<title>The Case for Mars</title>
<title>First Man: The Life of Neil A. Armstrong</title>
7.3
<title>The Ruby Way</title>
<title>The Case for Mars</title>
<title>First Man: The Life of Neil A. Armstrong</title>

读出 description
Second edition. The book you are now reading.Ain't recursion grand?
Pushing toward a second home for the human race.
Definitive biography of the first man on the moon.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: