demolisher 0.4.0 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +50 -1
- data/VERSION +1 -1
- data/demolisher.gemspec +4 -3
- data/lib/demolisher.rb +12 -7
- data/test/demolisher_test.rb +22 -0
- data/test/ns.xml +11 -0
- metadata +21 -9
data/README.rdoc
CHANGED
@@ -8,7 +8,6 @@ You can install using any of the following methods
|
|
8
8
|
|
9
9
|
$ gem install demolisher
|
10
10
|
$ gem install demolisher -s http://gemcutter.org
|
11
|
-
$ gem install geoffgarside-demolisher -s http://gems.github.com
|
12
11
|
|
13
12
|
the first two should always be stable, the latter github one should be stable but there are no guarantees.
|
14
13
|
|
@@ -54,6 +53,56 @@ and we should get the result of
|
|
54
53
|
this person is active
|
55
54
|
Randy Waterhouse: randy@example.com
|
56
55
|
|
56
|
+
== Namespaces
|
57
|
+
|
58
|
+
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.
|
59
|
+
|
60
|
+
As an example we have this document
|
61
|
+
|
62
|
+
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
|
63
|
+
<soap:Body>
|
64
|
+
<getManufacturerNamesResponse xmlns="http://services.somewhere.com">
|
65
|
+
<IDAndNameList xmlns="http://services.somewhere.com">
|
66
|
+
<ns1:IdAndName xmlns:ns1="http://domain.somewhere.com">
|
67
|
+
14-Demolisher
|
68
|
+
</ns1:IdAndName>
|
69
|
+
</IDAndNameList>
|
70
|
+
</getManufacturerNamesResponse>
|
71
|
+
</soap:Body>
|
72
|
+
</soap:Envelope>
|
73
|
+
|
74
|
+
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.
|
75
|
+
|
76
|
+
Additionally the list of namespaces need to known ahead of parse time. For the above document the list of namespaces is
|
77
|
+
|
78
|
+
1. http://schemas.xmlsoap.org/soap/envelope/ as soap
|
79
|
+
2. http://services.somewhere.com
|
80
|
+
3. http://domain.somewhere.com as ns1
|
81
|
+
|
82
|
+
as namespace two has no prefix, its a default namespace, we will assign it one when creating the hash of namespaces.
|
83
|
+
|
84
|
+
namespaces = {
|
85
|
+
'soap' => "http://schemas.xmlsoap.org/soap/envelope/",
|
86
|
+
'ns0' => "http://services.somewhere.com",
|
87
|
+
'ns1' => "http://domain.somewhere.com" }
|
88
|
+
|
89
|
+
this hash of namespaces is then passed to Demolisher
|
90
|
+
|
91
|
+
xml = Demolisher.demolish('soap.xml', namespaces)
|
92
|
+
xml.soap :Envelope do
|
93
|
+
xml.soap :Body do
|
94
|
+
xml.ns0 :getManufacturerNamesResponse do
|
95
|
+
xml.ns0 :IDAndNameList do
|
96
|
+
puts xml.ns1(:IdAndName).strip
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
the result of this will be
|
103
|
+
|
104
|
+
14-Demolisher
|
105
|
+
|
57
106
|
== Copyright
|
58
107
|
|
59
108
|
Copyright (c) 2009 Geoff Garside. See LICENSE for details.
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.5.0
|
data/demolisher.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{demolisher}
|
8
|
-
s.version = "0.
|
8
|
+
s.version = "0.5.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Geoff Garside"]
|
12
|
-
s.date = %q{
|
12
|
+
s.date = %q{2010-05-18}
|
13
13
|
s.email = %q{geoff@geoffgarside.co.uk}
|
14
14
|
s.extra_rdoc_files = [
|
15
15
|
"LICENSE",
|
@@ -25,6 +25,7 @@ Gem::Specification.new do |s|
|
|
25
25
|
"demolisher.gemspec",
|
26
26
|
"lib/demolisher.rb",
|
27
27
|
"test/demolisher_test.rb",
|
28
|
+
"test/ns.xml",
|
28
29
|
"test/test.xml",
|
29
30
|
"test/test_helper.rb"
|
30
31
|
]
|
@@ -32,7 +33,7 @@ Gem::Specification.new do |s|
|
|
32
33
|
s.rdoc_options = ["--charset=UTF-8"]
|
33
34
|
s.require_paths = ["lib"]
|
34
35
|
s.rubyforge_project = %q{demolisher}
|
35
|
-
s.rubygems_version = %q{1.3.
|
36
|
+
s.rubygems_version = %q{1.3.6}
|
36
37
|
s.summary = %q{Gem for extracting information from XML files, think Builder but backwards}
|
37
38
|
s.test_files = [
|
38
39
|
"test/demolisher_test.rb",
|
data/lib/demolisher.rb
CHANGED
@@ -2,9 +2,9 @@ require 'xml'
|
|
2
2
|
|
3
3
|
module Demolisher
|
4
4
|
# Demolish an XML file or XML::Parser object.
|
5
|
-
def self.demolish(file_or_xml_parser)
|
5
|
+
def self.demolish(file_or_xml_parser, namespace_list = nil)
|
6
6
|
file_or_xml_parser = new_parser(file_or_xml_parser) if file_or_xml_parser.kind_of?(String)
|
7
|
-
node = Node.new(file_or_xml_parser.parse, true)
|
7
|
+
node = Node.new(file_or_xml_parser.parse, namespace_list, true)
|
8
8
|
|
9
9
|
yield node if block_given?
|
10
10
|
node
|
@@ -26,9 +26,10 @@ module Demolisher
|
|
26
26
|
# Creates a new Node object.
|
27
27
|
#
|
28
28
|
# If the node is not the root node then the secondargument needs to be false.
|
29
|
-
def initialize(xml, is_root = true)
|
29
|
+
def initialize(xml, namespaces = nil, is_root = true)
|
30
30
|
@nodes = [xml]
|
31
31
|
@nodes.unshift(nil) unless is_root
|
32
|
+
@namespaces = namespaces
|
32
33
|
end
|
33
34
|
|
34
35
|
# Access an attribute of the current node.
|
@@ -62,7 +63,7 @@ module Demolisher
|
|
62
63
|
# looks like a boolean. Otherwise return text content
|
63
64
|
# Otherwise return a new Node instance
|
64
65
|
def method_missing(meth, *args, &block) # :nodoc:
|
65
|
-
xpath =
|
66
|
+
xpath = xpath_for_element(meth.to_s, args.shift)
|
66
67
|
return nil if xpath.empty?
|
67
68
|
|
68
69
|
if block_given?
|
@@ -88,7 +89,7 @@ module Demolisher
|
|
88
89
|
content
|
89
90
|
end
|
90
91
|
else
|
91
|
-
self.class.new(node, false)
|
92
|
+
self.class.new(node, @namespaces, false)
|
92
93
|
end
|
93
94
|
end
|
94
95
|
end
|
@@ -98,9 +99,13 @@ module Demolisher
|
|
98
99
|
@nodes.last.content.strip
|
99
100
|
end
|
100
101
|
|
102
|
+
def xpath_for_element(el_or_ns, el_for_ns = nil)
|
103
|
+
@nodes.last.find(element_from_symbol(el_or_ns, el_for_ns), @namespaces)
|
104
|
+
end
|
105
|
+
|
101
106
|
# Transforms a symbol into a XML element path.
|
102
|
-
def element_from_symbol(
|
103
|
-
"#{is_root_node? ? '/' : nil}#{
|
107
|
+
def element_from_symbol(el_or_ns,el_for_ns = nil) # :nodoc:
|
108
|
+
"#{is_root_node? ? '/' : nil}#{el_or_ns.gsub(/[^a-z0-9_]/i, '')}#{el_for_ns && el_for_ns.inspect}"
|
104
109
|
end
|
105
110
|
|
106
111
|
# Indicates if the current node is the root of the XML document.
|
data/test/demolisher_test.rb
CHANGED
@@ -61,4 +61,26 @@ EOXML
|
|
61
61
|
assert_equal 'Geoff', @demolisher.hi.there
|
62
62
|
end
|
63
63
|
end
|
64
|
+
context "Demolished XML file with Namespaces" do
|
65
|
+
setup do
|
66
|
+
@id_and_name = ''
|
67
|
+
@namespaces = {'soap' => "http://schemas.xmlsoap.org/soap/envelope/",
|
68
|
+
'ns0' => "http://services.somewhere.com",
|
69
|
+
'ns1' => "http://domain.somewhere.com" }
|
70
|
+
Demolisher.demolish(File.dirname(__FILE__) +'/ns.xml', @namespaces) do |xml|
|
71
|
+
xml.soap :Envelope do
|
72
|
+
xml.soap :Body do
|
73
|
+
xml.ns0 :getManufacturerNamesResponse do
|
74
|
+
xml.ns0 :IDAndNameList do
|
75
|
+
@id_and_name = xml.ns1(:IdAndName).strip
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
should "get '14-Demolisher' from XML file" do
|
83
|
+
assert_equal '14-Demolisher', @id_and_name
|
84
|
+
end
|
85
|
+
end
|
64
86
|
end
|
data/test/ns.xml
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
|
2
|
+
<soap:Body>
|
3
|
+
<getManufacturerNamesResponse xmlns="http://services.somewhere.com">
|
4
|
+
<IDAndNameList xmlns="http://services.somewhere.com">
|
5
|
+
<ns1:IdAndName xmlns:ns1="http://domain.somewhere.com">
|
6
|
+
14-Demolisher
|
7
|
+
</ns1:IdAndName>
|
8
|
+
</IDAndNameList>
|
9
|
+
</getManufacturerNamesResponse>
|
10
|
+
</soap:Body>
|
11
|
+
</soap:Envelope>
|
metadata
CHANGED
@@ -1,7 +1,12 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: demolisher
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 5
|
8
|
+
- 0
|
9
|
+
version: 0.5.0
|
5
10
|
platform: ruby
|
6
11
|
authors:
|
7
12
|
- Geoff Garside
|
@@ -9,19 +14,23 @@ autorequire:
|
|
9
14
|
bindir: bin
|
10
15
|
cert_chain: []
|
11
16
|
|
12
|
-
date:
|
17
|
+
date: 2010-05-18 00:00:00 +01:00
|
13
18
|
default_executable:
|
14
19
|
dependencies:
|
15
20
|
- !ruby/object:Gem::Dependency
|
16
21
|
name: libxml-ruby
|
17
|
-
|
18
|
-
|
19
|
-
version_requirements: !ruby/object:Gem::Requirement
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
20
24
|
requirements:
|
21
25
|
- - ">="
|
22
26
|
- !ruby/object:Gem::Version
|
27
|
+
segments:
|
28
|
+
- 1
|
29
|
+
- 1
|
30
|
+
- 3
|
23
31
|
version: 1.1.3
|
24
|
-
|
32
|
+
type: :runtime
|
33
|
+
version_requirements: *id001
|
25
34
|
description:
|
26
35
|
email: geoff@geoffgarside.co.uk
|
27
36
|
executables: []
|
@@ -41,6 +50,7 @@ files:
|
|
41
50
|
- demolisher.gemspec
|
42
51
|
- lib/demolisher.rb
|
43
52
|
- test/demolisher_test.rb
|
53
|
+
- test/ns.xml
|
44
54
|
- test/test.xml
|
45
55
|
- test/test_helper.rb
|
46
56
|
has_rdoc: true
|
@@ -56,18 +66,20 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
56
66
|
requirements:
|
57
67
|
- - ">="
|
58
68
|
- !ruby/object:Gem::Version
|
69
|
+
segments:
|
70
|
+
- 0
|
59
71
|
version: "0"
|
60
|
-
version:
|
61
72
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
62
73
|
requirements:
|
63
74
|
- - ">="
|
64
75
|
- !ruby/object:Gem::Version
|
76
|
+
segments:
|
77
|
+
- 0
|
65
78
|
version: "0"
|
66
|
-
version:
|
67
79
|
requirements: []
|
68
80
|
|
69
81
|
rubyforge_project: demolisher
|
70
|
-
rubygems_version: 1.3.
|
82
|
+
rubygems_version: 1.3.6
|
71
83
|
signing_key:
|
72
84
|
specification_version: 3
|
73
85
|
summary: Gem for extracting information from XML files, think Builder but backwards
|