Parsing XML with Ruby and Nokogiri
Posted
by
Chip Castle
on Stack Overflow
See other posts from Stack Overflow
or by Chip Castle
Published on 2010-06-21T22:21:51Z
Indexed on
2012/12/05
23:04 UTC
Read the original article
Hit count: 162
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?
© Stack Overflow or respective owner