使用Ruby和Nokogiri解析XML

时间:2022-06-21 00:34:36

I have the following XML structure:

我有以下XML结构:

<charsets>
  <charset>
    <name>ANSI_X3.4-1968</name>
    <aliases>
      <alias>iso-ir-6</alias>
      <alias>ANSI_X3.4-1986</alias>
      <alias>ISO_646.irv:1991</alias>
      <alias>ASCII</alias>
      <alias>ISO646-US</alias>
      <alias>US-ASCII</alias>
      <alias>us</alias>
      <alias>IBM367</alias>
      <alias>cp367</alias>
      <alias>csASCII</alias>
    </aliases>
  </charset>
  <charset>
    <name>ISO-10646-UTF-1</name>
    <aliases>
      <alias>csISO10646UTF1</alias>
    </aliases>
  </charset>
</charsets>

I can grab the text contents of the the name nodes using Ruby and Nokogiri using:

我可以使用Ruby和Nokogiri获取名称节点的文本内容:

require 'nokogiri'
require 'open-uri'

doc = Nokogiri::XML(File.open("StandardCharsets.xml"))
@charsets = doc.css("charsets name").map {|node| node.children.text }

But, what I want is the text contents of all name and alias nodes in the order as they are shown in the source document. Everything I try fails.

但是,我想要的是源文档中显示的所有名称和别名节点的文本内容。所有我尝试失败。

Does anyone have a good example of how to do this?

有没有人有一个很好的例子来说明如何做到这一点?

1 个解决方案

#1


6  

Change the line

改变行

@charsets = doc.css("charsets name").map {|node| node.children.text }

to

@charsets = doc.css("charsets name, charsets alias").map {|node| node.children.text }

#1


6  

Change the line

改变行

@charsets = doc.css("charsets name").map {|node| node.children.text }

to

@charsets = doc.css("charsets name, charsets alias").map {|node| node.children.text }