Skip to content

Instantly share code, notes, and snippets.

@jamster
Created September 2, 2009 21:45
Show Gist options
  • Select an option

  • Save jamster/179981 to your computer and use it in GitHub Desktop.

Select an option

Save jamster/179981 to your computer and use it in GitHub Desktop.
require "rubygems"
require "crack"
xml = <<-XML
<parent_node>
<content>
<item attr1="test">this is a string that ruins my life</item>
<item attr1="i am an attribute and i live nicely in the world of hashes" />
</content>
</parent_node>
XML
parsed_xml = Crack::XML.parse(xml)
bad_item = parsed_xml['parent_node']['content']['item'][0]
good_item = parsed_xml['parent_node']['content']['item'][1]
puts bad_item.class
# String
puts good_item.class
# Hash
puts bad_item.instance_variables
# @attributes
puts bad_item.instance_variable_get('@attributes').class
#Hash
puts bad_item.instance_variable_get('@attributes').to_yaml
# ---
# attr1: test
puts parsed_xml.to_yaml
# ---
# parent_node:
# content:
# item:
# - !str
# str: this is a string that ruins my life
# "@attributes":
# attr1: test
# - attr1: i am an attribute and i live nicely in the world of hashes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment