No commit activity in last 3 years
No release in over 3 years
Gem for extracting information from XML files, think Builder but backwards
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Runtime

>= 1.1.3
 Project Readme

Demolisher¶ ↑

Works in a similar fashion to Builder but is instead used for extracting information from XML files rather than building them.

Installation¶ ↑

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.

Example¶ ↑

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>enoch@example.com</email>
    </contact>
    <active>YES</active>
  </person>
  <person>
    <firstname>Randy</firstname>
    <lastname>Waterhouse</lastname>
    <contact>
      <phone>01234 567 8901</phone>
      <email>randy@example.com</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: enoch@example.com
this person is active
Randy Waterhouse: randy@example.com

Namespaces¶ ↑

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

  1. schemas.xmlsoap.org/soap/envelope/ as soap

  2. services.somewhere.com

  3. domain.somewhere.com as ns1

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.