操作XML檔案

2021-06-01 20:14:16 字數 2945 閱讀 1552

# 操作函式:

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 el = root.add_element("mytest")

#在mytest下面建立子節點元素 another 並且賦值id =10 #結果如下:el2 = el.add_element "another",

#建立乙個元素節點 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.elementshal fulton

second edition. the book you are now reading.ain't recursion grand?

the case for mars

robert zubrin

pushing toward a second home for the human race.

first man: the life of neil a. armstrong

james r. hansen

definitive biography of the first man on the moon.

操作結果:

sean

hello dolly

銆??銆??銆??銆?

0銆??銆?銆??銆??銆??銆?銆??銆??銆??銆?the ruby way

銆??銆??銆??銆?hal fulton

銆??銆??銆??銆?second edition. the book you are now reading.ain't recursion grand?

銆??銆??銆??銆?

銆??銆?

銆??銆?銆??銆??銆??銆?銆??銆??銆??銆??銆??銆?the case for mars

銆??銆??銆??銆??銆??銆?robert zubrin

銆??銆??銆??銆??銆??銆?pushing toward a second home for the human race.

銆??銆??銆??銆?

銆??銆??銆??銆?銆??銆??銆??銆??銆??銆?first man: the life of neil a. armstrong

銆??銆??銆??銆??銆??銆?james r. hansen

銆??銆??銆??銆??銆??銆?definitive biography of the first man on the moon.

銆??銆??銆??銆?

銆??銆?

sean

hello dolly

1銆??銆??銆??銆?the ruby way

銆??銆??銆??銆?hal fulton

銆??銆??銆??銆?second edition. the book you are now reading.ain't recursion grand?

銆??銆??銆??銆?

2.1the ruby way

the case for mars

first man: the life of neil a. armstrong

2.2the ruby way

the case for mars

first man: the life of neil a. armstrong

3.1hal fulton

robert zubrin

james r. hansen

3.2hal fulton

robert zubrin

james r. hansen

3.3hal fulton

robert zubrin

james r. hansen

ruby

space

0672328844

0684835509

074325631x

7.1the ruby way

the case for mars

first man: the life of neil a. armstrong

7.2the ruby way

the case for mars

first man: the life of neil a. armstrong

7.3the ruby way

the case for mars

first man: the life of neil a. armstrong

讀出 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.

操作XML檔案

1 操作xml檔案的類庫位於system.xml命名空間下 xmlnode 是乙個抽象類,代表乙個節點。其中document,element,attribute,text 等都是特定的節點型別。注意其實現的幾個介面。xmlnodelist是乙個比較有用的集合類 xmldocument 代表乙個dom...

Xml檔案操作

using system using system.text using system.xml class xmloperate private void readxmlfile string filename private void addelement xmldocument xmldocum...

Python操作XML檔案 XML概述

xml 可擴充套件標置語言,為html 超文字標置語言 的補充。html用於顯示資料,而xml用於傳輸和儲存資料 一.xml語法 xml檔案通常分為兩部分 檔案宣告和檔案主體 檔案宣告 位於第一行 version標明此檔案所用的標準的版本號,必須要有 encoding標明此檔案中所使用的字元型別,可...