Works in a similar fashion to Builder but is instead used for extracting information from XML files rather than building them.
You can install using any of the following methods
$ gem install demolisher $ gem install demolisher -s http://gemcutter.org
the first two should always be stable, the latter github one should be stable but there are no guarantees.
Given the simple XML example file below
<addressbook version="1.0"> <person> <firstname>Enoch</firstname> <lastname>Root</lastname> <contact> <phone>01234 567 8900</phone> <email>[email protected]</email> </contact> <active>YES</active> </person> <person> <firstname>Randy</firstname> <lastname>Waterhouse</lastname> <contact> <phone>01234 567 8901</phone> <email>[email protected]</email> </contact> <active>NO</active> </person> </addressbook>
we can parse it with
xml = Demolisher.demolish('addressbook.xml') xml.addressbook do puts "Version #{xml['version']}" xml.person do puts "#{xml.firstname} #{xml.lastname}: #{xml.contact.email}" puts "this person is active" if xml.active? end end
and we should get the result of
Enoch Root: [email protected] this person is active Randy Waterhouse: [email protected]
There is now rudimentary support for XML namespaced documents. The caveat is that if you are using a document with any XML namespaced elements you must access all elements via a namespace prefix.
As an example we have this document
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Body> <getManufacturerNamesResponse xmlns="http://services.somewhere.com"> <IDAndNameList xmlns="http://services.somewhere.com"> <ns1:IdAndName xmlns:ns1="http://domain.somewhere.com"> 14-Demolisher </ns1:IdAndName> </IDAndNameList> </getManufacturerNamesResponse> </soap:Body> </soap:Envelope>
some of the elements define their default namespace, getManufacturerNamesResponse
and IDAndNameList
so are not prefixed. When accessing these elements they will need to be prefixed.
Additionally the list of namespaces need to known ahead of parse time. For the above document the list of namespaces is
as namespace two has no prefix, its a default namespace, we will assign it one when creating the hash of namespaces.
namespaces = { 'soap' => "http://schemas.xmlsoap.org/soap/envelope/", 'ns0' => "http://services.somewhere.com", 'ns1' => "http://domain.somewhere.com" }
this hash of namespaces is then passed to Demolisher
xml = Demolisher.demolish('soap.xml', namespaces) xml.soap :Envelope do xml.soap :Body do xml.ns0 :getManufacturerNamesResponse do xml.ns0 :IDAndNameList do puts xml.ns1(:IdAndName).strip end end end end
the result of this will be
14-Demolisher
Copyright © 2009 Geoff Garside. See LICENSE for details.