Ruby教程
Ruby控制语句
Ruby高级

Ruby XML(REXML)

Ruby XML(REXML)

XML 是类似于 HTML 的可扩展标记语言。它允许程序员开发可以被其他应用程序读取的应用程序,而不管使用的操作系统和开发语言如何。
它跟踪小到中等数量的数据,而无需在后端使用任何基于 SQL 的技术。
REXML 是一个纯 Ruby XML 处理器。它代表一个完整的 XML 文档,包括 PI、doctype 等。一个 XML 文档有一个可以被 root() 访问的子文档。如果要为创建的文档添加 XML 声明,则必须添加一个。 REXML 文档不会为您编写默认声明。
REXML 的灵感来自于 Java 的 Electric XML 库。它的 API 易于使用,体积小,并且在方法命名和代码流方面遵循 Ruby 方法论。
它同时支持树和流文档解析。 Steam 解析比树解析快 1.5 倍。但是,在流解析中,您无法访问 XPath 等某些功能。

REXML 特性:

它是 100% 用 Ruby 编写的。 它包含的代码少于 2000 行,因此重量更轻。 它的方法和类很容易理解。 它随 Ruby 安装一起提供。无需单独安装。 它用于 DOM 和 SAX 解析。

解析 XML 并访问元素

让我们从解析 XML 文档开始:
require "rexml/document" file = File. new( "trial.xml" ) doc = REXML::Document.new file
在上面的代码中,第 3 行解析提供的文件。
示例:
require 'rexml/document' include REXML file = File. new( "trial.xml") doc = Document. new(file) puts docs
在上面的代码中,require 语句加载了 REXML 库。然后包含 REXML 表示我们不必使用像 REXML::Document 这样的名称。我们已经创建了trial.xml 文件。文件显示在屏幕上。
输出:
Ruby XML 1
Document.new 方法以 IO、String 对象或 Document 作为参数。此参数指定必须从中读取 XML 文档的源。
如果 Document 构造函数将 Document 作为参数,则其所有元素节点都将克隆到新的 Document 对象。如果构造函数采用 String 参数,则 string 将包含 XML 文档。

带有"此处文档"的 XML

此处文档是一种指定文本块、保留换行符、空格或文本标识的方法。
此处的文档是使用命令后跟"<<"后跟标记字符串来构造的。
在 Ruby 中,"<<"和标记字符串之间不应有空格。
示例:
#!/usr/bin/env ruby require 'rexml/document' include REXML info = <<XML <info> <name>Caroline</name> <street>9820 St.</street> <city>Seattle</city> <contact>9854126575</contact> <country>USA</country> </info> XML document = Document. new( info ) puts document
这里,我们在这里使用文档信息。 <<EOF 和 EOF 之间的所有字符包括换行符都是信息的一部分。
对于 XML 解析示例,我们将使用以下 XML 文件代码作为输入:
file trial .xml
#!/usr/bin/ruby-w require 'rexml/document' include REXML xmlfile = File. new( "trial.xml") xmldoc = Document. new(xmlfile) # Now get the root element root = xmldoc.root puts "Root element : " + root.attributes[ "shelf"] # this will output all the cloth titles. xmldoc. elements.each( "collection/clothing"){ |e| puts "cloth Title : " + e.attributes[ "title"] } # this will output all the cloth types. xmldoc. elements.each( "collection/clothing/type") { |e| puts "cloth Type : " + e.text } # this will output all the cloth description. xmldoc. elements.each( "collection/clothing/description") { |e| puts "cloth Description : " + e.text }

Ruby XML DOM-Like Parsing

我们将以树的方式解析我们的 XML 数据。以上文件trial.xml代码作为输入。
#!/usr/bin/ruby-w require 'rexml/document' include REXML xmlfile = File. new( "trial.xml") xmldoc = Document. new(xmlfile) # Now get the root element root = xmldoc.root puts "Root element : " + root.attributes[ "shelf"] # this will output all the cloth titles. xmldoc. elements.each( "collection/clothing"){ |e| puts "cloth Title : " + e.attributes[ "title"] } # this will output all the cloth types. xmldoc. elements.each( "collection/clothing/type") { |e| puts "cloth Type : " + e.text } # this will output all the cloth description. xmldoc. elements.each( "collection/clothing/description") { |e| puts "cloth Description : " + e.text }
输出:
Ruby XML 2

Ruby XML SAX-Like 解析

我们将以流方式解析我们的 XML 数据。上面的文件trial.xml 代码作为输入。在这里,我们将定义一个侦听器类,其方法将针对解析器的回调。
建议不要对小文件使用类似 SAX 的解析。
#!/usr/bin/ruby-w 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 xmlfile = File. new( "trial.xml") Document. parse_stream(xmlfile, list)
输出:
Ruby XML 3
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4