开发者

Parsing XML with Ruby and Nokogiri

I have the following XML structure:

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

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?


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜