maik / xml-simple

Easy API for working with XML documents
MIT License
88 stars 27 forks source link

XmlSimple.xml_in seems to drop the first element of an XML tag #21

Closed FilBot3 closed 8 years ago

FilBot3 commented 8 years ago

When I use XmlSimple v1.1.5, I import an XML file like the one below, and it seems to drop the first element of the XML tag as seen in the Hash output towards the bottom. Is there some option I'm missing?

λ cat search_by_change_id.xml
<inv:SearchByChangeRequestID
  xmlns:inv="http://Remedy_Change_Request_LIB/Invoke"
  xmlns:wser="http://Remedy_Change_Request_LIB">
  <request>
    <ChangeRequestID>CRQ000000905650</ChangeRequestID>
  </request>
</inv:SearchByChangeRequestID>
irb(main):008:0> require 'xmlsimple'
=> true
irb(main):009:0> hash = XmlSimple.xml_in(File.open("search_by_change_id.xml"))
=> {"xmlns:inv"=>"http://Remedy_Change_Request_LIB/Invoke", "xmlns:wser"=>"http://Remedy_Change_Request_LIB", "request"=>[{"ChangeRequestID"=>["CRQ000000905650"]}]}
irb(main):010:0> pp hash
{"xmlns:inv"=>"http://Remedy_Change_Request_LIB/Invoke",
 "xmlns:wser"=>"http://Remedy_Change_Request_LIB",
 "request"=>[{"ChangeRequestID"=>["CRQ000000905650"]}]}
=> {"xmlns:inv"=>"http://Remedy_Change_Request_LIB/Invoke", "xmlns:wser"=>"http://Remedy_Change_Request_LIB", "request"=>[{"ChangeRequestID"=>["CRQ000000905650"]}]}
irb(main):011:0>
maik commented 8 years ago

The option you're missing is KeepRoot as described in the documentation (http://xml-simple.rubyforge.org). So, you could use

hash = XmlSimple.xml_in(File.open("search_by_change_id.xml"), :KeepRoot => true)

Keep in mind, though, that XmlSimple does not work well with XML namespaces that is the result will be

{"SearchByChangeRequestID"=>[{"xmlns:inv"=>"http://Remedy_Change_Request_LIB/Invoke", "xmlns:wser"=>"http://Remedy_Change_Request_LIB", "request"=>[{"ChangeRequestID"=>["CRQ000000905650"]}]}]}