|
Ruby Cookbook Authors: Carlson L., Richardson L. Published year: Pages: 176/399 |
Recipe 11.2. Extracting Data from a Document's Tree StructureCredit: Rod Gaither ProblemYou want to parse an XML file into a Ruby data structure, to traverse it or extract data from it. SolutionPass an XML document into the REXML::Document constructor to load and parse the XML. A Document object contains a tree of subobjects (of class Element and Text ) rep-resenting the tree structure of the underlying document. The methods of Document and Element give you access to the XML tree data. The most useful of these methods is #each_element . Here's some sample XML and the load process. The document describes a set of orders, each of which contains a set of items. This particular document contains a single order for two items.
orders_xml = %{
<orders>
<order>
<number>105</number>
<date>02/10/2006</date>
<customer>Corner Store</customer>
<items>
<item upc="404100" desc="Red Roses" qty="240" />
<item upc="412002" desc="Candy Hearts" qty="160" />
</items>
</order>
</orders>}
require 'rexml/document'
orders = REXML::Document.new(orders_xml)
To process each order in this document, we can use Document#root to get the document's root element ( <orders> )and then call Element#each_element to iterate over the children of the root element (the <order> elements). This code repeatedly calls each to move down the document tree and print the details of each order in the document:
orders.root.each_element do order # each <order> in <orders>
order.each_element do node # <customer>, <items>, etc. in <order>
if node.has_elements?
node.each_element do child # each <item> in <items>
puts "#{child.name}: #{child.attributes['desc']}"
end
else
# the contents of <number>, <date>, etc.
puts "#{node.name}: #{node.text}"
end
end
end
# number: 105
# date: 02/10/2006
# customer: Corner Store
# item: Red Roses
# item: Candy Hearts
DiscussionParsing an XML file into a Document gives you a tree-like data structure that you can treat kind of like an array of arrays. Starting at the document root, you can move down the tree until you find the data that interests you. In the example above, note how the structure of the Ruby code mirrors the structure of the original document. Every call to each_element moves the focus of the code down a level: from <orders> to <order> to <items> to <item> . There are many other methods of Element you can use to navigate the tree structure of an XML document. Not only can you iterate over the child elements, you can reference a specific child by indexing the parent as though it were an array. You can navigate through siblings with Element.next_element and Element.previous_element . You can move up the document tree with Element.parent : my_order = orders.root.elements[1] first_node = my_order.elements[1] first_node.name # => "number" first_node.next_element.name # => "date" first_node.parent.name # => "order" This only scratches the surface; there are many other ways to interact with the data loaded from an XML source. For example, explore the convenience methods Element.each_element_with_attribute and Element.each_element_with_text , which let you select elements based on features of the elements themselves . See Also
|
|
|
|
Ruby Cookbook Authors: Carlson L., Richardson L. Published year: Pages: 176/399 |