Created
September 2, 2009 21:45
-
-
Save jamster/179981 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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