Skip to content


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP


Exception from the special treatment of XML tags with type == file #40

merged 1 commit into from

2 participants


I was parsing an XML document (in this case, the XML configuration of a kvm domain) with Crack::XML.parse and it was throwing an exception.

After digging around this was because in the case where the tag has a type attribute set to file the children nodes are assumed to be strings. In this case the children are another XML tag and not a string causing an exception.

I also added a test as requested covering this bug.

@jnunemaker jnunemaker merged commit b176993 into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Oct 20, 2012
  1. @admgre

    Fixed an issue parsing xml file where a node with type == file has ch…

    admgre authored
    …ildren that are not strings
This page is out of date. Refresh to see the latest.
Showing with 15 additions and 2 deletions.
  1. +2 −1  lib/crack/xml.rb
  2. +13 −1 test/xml_test.rb
3  lib/crack/xml.rb
@@ -70,7 +70,8 @@ def add_node(node)
def to_hash
- if @type == "file"
+ # ACG: Added a check here to prevent an exception a type == "file" tag has nodes within it
+ if @type == "file" and (@children.first.nil? or @children.first.is_a?(String))
f = || '').unpack('m').first)
class << f
attr_accessor :original_filename, :content_type
14 test/xml_test.rb
@@ -496,4 +496,16 @@ class XmlTest < Test::Unit::TestCase
should "handle an xml string containing a single space" do
Crack::XML.parse(' ').should == {}
+ should "handle a node with type == file that has children" do
+ # Example is an excerpt from a problematic kvm domain config file
+ example_xml = <<-EOT
+ <disk type='file' device='cdrom'>
+ <driver name='qemu' type='raw' cache='none' io='native'/>
+ <source file='/tmp/cdrom.iso'/>
+ </disk>
+ Crack::XML.parse(example_xml).should == {"disk"=>{"driver"=>{"name"=>"qemu", "type"=>"raw", "cache"=>"none", "io"=>"native"}, "source"=>{"file"=>"/tmp/cdrom.iso"}, "type"=>"file", "device"=>"cdrom"}}
+ end
Something went wrong with that request. Please try again.