xml-to-hash 0.9.0 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 31d59b40fb3dc7a1871e7c57e920ada8ddb17409
4
- data.tar.gz: 8ac9f306b1583f3337df70137d45b116a7f55cad
3
+ metadata.gz: 08fb0fa4983e65846ebd00cd4ce754b0c0a957e5
4
+ data.tar.gz: e9db1b95fbf058631078642e5828ef07e7af46e5
5
5
  SHA512:
6
- metadata.gz: 1a34928339763a712c247ac5a4422d35145ad3bb19b0c2b1f40aa21b16255db10b5983831a12304948d94004b273fd68019d8900231fc686982f13b3fc564020
7
- data.tar.gz: 65f3a4203ff7d1401b517a27b8a69bdaf69f07b756a82d2eb61991b3477ef0a75dea09b7919d389c27582610ee0bc47cc58173787a9c3fd4b8b297a28b630a01
6
+ metadata.gz: d5915f6d12e161143e5cb779d7318087b614cb0981b636ad0c8d19dade9117b7b0a837e292028cdda1b43ac2c1cd664ec0e973cdcc5d9c39df37a65bfa89a3d7
7
+ data.tar.gz: 2e568875fef31d96dca31cf52224608af28056991f60285cdcc1d2f53d7674809ce594983867c5862ed9c5aa4c19b0dc85c49880a2f93a605ab8b155c45143b8
File without changes
File without changes
data/README ADDED
@@ -0,0 +1,221 @@
1
+ # XML to Hash
2
+
3
+ This Ruby gem adds a `to_hash` method to Nokogiri XML nodes, allowing us to convert arbitrary XML nodes to a Ruby hash,
4
+ and so also to serialize them to JSON.
5
+
6
+ This gem also picks up attributes, processing instructions and doctype declarations. The resulting hash is wordy, but complete.
7
+
8
+ As an added bonus, we include line numbers where possible.
9
+
10
+
11
+ ## Installation
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ ```ruby
16
+ gem 'xml-to-hash'
17
+ ```
18
+
19
+ And then execute:
20
+
21
+ $ bundle
22
+
23
+ Or install it yourself as:
24
+
25
+ $ gem install xml-to-hash
26
+
27
+ ## Usage
28
+
29
+ ```ruby
30
+ include 'xml/to/hash'
31
+
32
+ xml_string = STR_XML = <<-EOS
33
+ <!DOCTYPE dtd-name [
34
+ <!ENTITY entity_1 "Has been ëxpÄnded">
35
+ <!ENTITY entity_system SYSTEM "mt_cook1.jpg">
36
+ <!ENTITY name_public PUBLIC "entity_public_id" "URI">
37
+ <!NOTATION notation-system SYSTEM "notation-id-system">
38
+ <!NOTATION NoTaTiOn-PuBLiC PUBLIC "notation-id-public">
39
+ <!ELEMENT div1 (head,
40
+ (p | list | note)*,
41
+ div2*)>
42
+ <!ELEMENT photo (hello)>
43
+ <!ATTLIST photo some-attribute CDATA #REQUIRED>
44
+ <!ATTLIST
45
+ photo photo_att ENTITY #IMPLIED
46
+ photo NOTATION (notation-system | NoTaTiOn-PuBLiC | notation-system) #IMPLIED>
47
+ ]>
48
+
49
+ <myRoot xml:id="root" xml:lang="en">
50
+ some text
51
+ <!--
52
+ In comments we can use ]]>
53
+ <
54
+ &,
55
+ ',
56
+ and ",
57
+ but %MyParamEntity; will not be expanded-->
58
+ <![CDATA[
59
+ Character Data block <!-- <,
60
+ & ' " --> *and* %MyParamEntity;
61
+ ]]>
62
+ <?linebreak?>
63
+ <deeper xmlns="lol://some-namespace" how-deep="very-deep">☠☠☠randomtext☠☠☠
64
+ <even
65
+ lol:my-attr="just an attribute"
66
+ xmlns:lol=\'lol://my.name.space/\' deeper="true">&amp;</even></deeper>
67
+ </myRoot>
68
+ EOS
69
+
70
+ xml = Nokogiri::XML STR_XML
71
+ hash = xml.root.to_hash # Use xml.to_hash for information about the document, like DTD and stuff
72
+
73
+ puts JSON.pretty_generate(hash)
74
+ ```
75
+
76
+ produces
77
+
78
+ ```json
79
+ {
80
+ "type": "element",
81
+ "name": "myRoot",
82
+ "attributes": [
83
+ {
84
+ "type": "attribute",
85
+ "name": "id",
86
+ "content": "root",
87
+ "line": 17,
88
+ "namespace": {
89
+ "href": "http://www.w3.org/XML/1998/namespace",
90
+ "prefix": "xml"
91
+ }
92
+ },
93
+ {
94
+ "type": "attribute",
95
+ "name": "lang",
96
+ "content": "en",
97
+ "line": 17,
98
+ "namespace": {
99
+ "href": "http://www.w3.org/XML/1998/namespace",
100
+ "prefix": "xml"
101
+ }
102
+ }
103
+ ],
104
+ "line": 17,
105
+ "children": [
106
+ {
107
+ "type": "text",
108
+ "content": "\n some text\n ",
109
+ "line": 19
110
+ },
111
+ {
112
+ "type": "comment",
113
+ "content": "\n In comments we can use ]]>\n <\n &,\n ',\n and \",\n but %MyParamEntity; will not be expanded",
114
+ "line": 25
115
+ },
116
+ {
117
+ "type": "text",
118
+ "content": "\n ",
119
+ "line": 26
120
+ },
121
+ {
122
+ "type": "cdata",
123
+ "name": "#cdata-section",
124
+ "content": "\n Character Data block <!-- <,\n & ' \" --> *and* %MyParamEntity;\n ",
125
+ "line": 26
126
+ },
127
+ {
128
+ "type": "text",
129
+ "content": "\n ",
130
+ "line": 30
131
+ },
132
+ {
133
+ "type": "pi",
134
+ "name": "linebreak",
135
+ "line": 30
136
+ },
137
+ {
138
+ "type": "text",
139
+ "content": "\n ",
140
+ "line": 31
141
+ },
142
+ {
143
+ "type": "element",
144
+ "name": "deeper",
145
+ "attributes": [
146
+ {
147
+ "type": "attribute",
148
+ "name": "how-deep",
149
+ "content": "very-deep",
150
+ "line": 31
151
+ }
152
+ ],
153
+ "line": 31,
154
+ "namespace": {
155
+ "href": "lol://some-namespace"
156
+ },
157
+ "children": [
158
+ {
159
+ "type": "text",
160
+ "content": "☠☠☠randomtext☠☠☠\n ",
161
+ "line": 32
162
+ },
163
+ {
164
+ "type": "element",
165
+ "name": "even",
166
+ "attributes": [
167
+ {
168
+ "type": "attribute",
169
+ "name": "my-attr",
170
+ "content": "just an attribute",
171
+ "line": 34,
172
+ "namespace": {
173
+ "href": "lol://my.name.space/",
174
+ "prefix": "lol"
175
+ }
176
+ },
177
+ {
178
+ "type": "attribute",
179
+ "name": "deeper",
180
+ "content": "true",
181
+ "line": 34
182
+ }
183
+ ],
184
+ "line": 34,
185
+ "namespace": {
186
+ "href": "lol://some-namespace"
187
+ },
188
+ "children": [
189
+ {
190
+ "type": "text",
191
+ "content": "&",
192
+ "line": 34
193
+ }
194
+ ]
195
+ }
196
+ ]
197
+ },
198
+ {
199
+ "type": "text",
200
+ "content": "\n ",
201
+ "line": 35
202
+ }
203
+ ]
204
+ }
205
+ ```
206
+
207
+ ## Development
208
+
209
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
210
+
211
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
212
+
213
+ ## Contributing
214
+
215
+ Bug reports and pull requests are welcome on GitHub at https://github.com/digitalheir/ruby-xml-to-hash. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](CODE_OF_CONDUCT).
216
+
217
+
218
+ ## License
219
+
220
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
221
+
@@ -1,18 +1,106 @@
1
- require 'xml/to/hash/version'
2
1
  require 'nokogiri'
3
2
 
4
3
  module Xml
4
+ # noinspection RubyClassModuleNamingConvention
5
5
  module To
6
6
  module Hash
7
+ VERSION = '1.0.1'
8
+
9
+ def add_children!(node, hash, blacklist=[])
10
+ unless node.respond_to? :node_type and blacklist.include? node.node_type
11
+ if node.children and node.children.length > 0
12
+ children_nodes=[]
13
+ node.children.each do |child|
14
+ children_nodes << child.to_hash
15
+ end
16
+ hash[:children]=children_nodes
17
+ end
18
+ end
19
+ end
7
20
  end
8
21
  end
9
22
  end
23
+
10
24
  module Nokogiri
11
25
  module XML
12
- class Node
26
+ class Notation
27
+ def to_hash
28
+ hash = {
29
+ }
30
+ add_if_respond_to!(hash, :name)
31
+ add_if_respond_to!(hash, :public_id)
32
+ add_if_respond_to!(hash, :system_id)
33
+ hash
34
+ end
35
+
36
+ private
37
+ def add_if_respond_to!(hash, method)
38
+ if respond_to? method and self.send method
39
+ hash[method]= self.send method
40
+ end
41
+ hash
42
+ end
43
+ end
44
+ class Namespace
45
+ include Xml::To::Hash
13
46
 
47
+ def to_hash
48
+ hash = {
49
+ href: href
50
+ }
51
+ if prefix
52
+ hash[:prefix] = prefix
53
+ end
54
+ hash
55
+ end
56
+ end
57
+ class ElementContent
58
+ include Xml::To::Hash
59
+
60
+ def to_hash
61
+ hash = {}
62
+ if name
63
+ hash[:name] = name
64
+ end
65
+ if prefix
66
+ hash[:prefix] = prefix
67
+ end
68
+ if occur
69
+ case occur
70
+ when ONCE
71
+ hash[:occur] = :once
72
+ when OPT
73
+ hash[:occur] = :opt
74
+ when MULT
75
+ hash[:occur] = :mult
76
+ when PLUS
77
+ hash[:occur] = :plus
78
+ else
79
+ raise "Could not handle occur value #{occur}"
80
+ end
81
+ end
82
+ if type
83
+ case type
84
+ when PCDATA
85
+ hash[:type] = :pcdata
86
+ when ELEMENT
87
+ hash[:type] = :element
88
+ when SEQ
89
+ hash[:type] = :seq
90
+ when OR
91
+ hash[:type] = :or
92
+ else
93
+ raise "Could not handle type value #{occur}"
94
+ end
95
+ end
96
+ add_children! self, hash
97
+ hash
98
+ end
99
+ end
100
+ class Node
101
+ include Xml::To::Hash
14
102
  # Returns node type as symbol instead of integer
15
- def self.get_type int
103
+ def self.get_type(int)
16
104
  case int
17
105
  # Element node type, see Nokogiri::XML::Node#element?
18
106
  when Nokogiri::XML::Node::ELEMENT_NODE
@@ -78,71 +166,119 @@ module Nokogiri
78
166
  when Nokogiri::XML::Node::DOCB_DOCUMENT_NODE
79
167
  return :docb_document
80
168
  else
81
- return int
169
+ raise "Could not handle node type #{int}"
82
170
  end
83
171
  end
84
172
 
173
+ def set_attributes(hash)
174
+ hash = hash.clone
175
+ set_attributes!(hash)
176
+ hash
177
+ end
178
+
179
+ def set_attributes!(hash)
180
+ if respond_to? :attribute_nodes and attribute_nodes.length > 0
181
+ hash[:attributes] = []
182
+ attribute_nodes.each do |attr|
183
+ hash[:attributes] << attr.to_hash
184
+ end
185
+ end
186
+ hash
187
+ end
188
+
85
189
  # Serialize this Node to a hash
86
190
  #
87
191
  # Example:
88
- # >> Nokogiri::XML '<xml>hello</xml>'
192
+ # >> Nokogiri::XML('<xml>hello</xml>').root.to_hash
89
193
  # => {:type=>:element, :name=>"xml", :children=>[{:type=>:text, :content=>"hello"}]}
90
194
  def to_hash
91
- Node.obj_for_node root
92
- end
93
-
94
- private
95
- # Given a Nokigiri XML node, create a Ruby hash
96
- def self.obj_for_node node
97
- ret = {
98
- type: get_type(node.node_type),
99
- }
100
- if node.attributes and node.attributes.length > 0
101
- ret[:attrs] = []
102
- node.attributes
103
- node.attributes.each do |key|
104
- attr = key[1]
105
- attr_o = {
106
- name: key[0],
107
- value: attr.content
108
- }
109
- if attr.namespace
110
- attr_o[:namespace] = namespace_hash(attr.namespace)
195
+ def set_if_respond_to!(hash, meth, blacklist=[])
196
+ unless blacklist.include? node_type
197
+ if respond_to? meth
198
+ val = send(meth)
199
+ if val
200
+ if Node.get_type(node_type).to_s == val
201
+ puts "Consider blacklisting #{val} for #{meth}"
202
+ end
203
+ if val.respond_to? :to_hash
204
+ val = val.to_hash
205
+ end
206
+ hash[meth] = val
207
+ end
111
208
  end
112
- ret[:attrs] << attr_o
113
209
  end
114
210
  end
115
- # Treat elements a little bit differently
116
- case node.node_type
117
- when Nokogiri::XML::Node::ELEMENT_NODE, Nokogiri::XML::Node::PI_NODE
118
- ret[:name] = node.name
119
- else
120
- ret[:content]= node.content
121
- end
122
- if node.namespace
123
- ret[:namespace] = namespace_hash(node.namespace)
124
- end
125
211
 
126
- # Recurse into children
127
- if node.children and node.children.length > 0
128
- unless node.element?
129
- puts "W-What? Node had children, but was not an element (but a #{get_type node.node_type})"
212
+ # Helper functions
213
+ def set_object_array!(hash, meth)
214
+ if respond_to? meth and send(meth) and send(meth).length > 0
215
+ hash[meth] = []
216
+ array = send(meth)
217
+ case meth
218
+ when :entities, :elements, :notations
219
+ array = array.values
220
+ else
221
+ end
222
+ array.each do |el|
223
+ hash[meth] << el.to_hash
224
+ end
130
225
  end
131
- ret[:children]=[]
132
- node.children.each do |child|
133
- ret[:children] << obj_for_node(child)
226
+ hash
227
+ end
228
+
229
+ def set_content!(hash, blacklist=[])
230
+ unless blacklist.include? node_type
231
+ if respond_to? :content and content
232
+ c = content
233
+ if c.class == String
234
+ hash[:content] = c
235
+ elsif c.is_a? Nokogiri::XML::ElementContent
236
+ hash[:content] = c.to_hash
237
+ else
238
+ raise "Could not handle content class #{c.class}"
239
+ end
240
+ end
134
241
  end
242
+ hash
135
243
  end
136
- ret
137
- end
138
244
 
139
- def self.namespace_hash(namespace)
245
+ # Initialize hash
140
246
  hash = {
141
- href: namespace.href
247
+ type: Node.get_type(node_type),
142
248
  }
143
- if namespace.prefix
144
- hash[:prefix] = namespace.prefix
249
+
250
+ # Treat elements a little bit differently
251
+ case node_type
252
+ when Nokogiri::XML::Node::DTD_NODE
253
+ set_object_array!(hash, :elements)
254
+ set_object_array!(hash, :entities)
255
+ set_object_array!(hash, :notations)
256
+ when Nokogiri::XML::Node::ELEMENT_DECL
257
+ puts 'lol'
258
+ when Nokogiri::XML::Node::ENTITY_DECL
259
+ set_if_respond_to! hash, :original_content
260
+ else
261
+ end
262
+
263
+ set_if_respond_to! hash, :name, [Node::DOCUMENT_NODE, Node::TEXT_NODE, Node::COMMENT_NODE]
264
+ set_if_respond_to! hash, :external_id # For DTD, entity declarations
265
+ set_if_respond_to! hash, :entity_type # For Entity declarations
266
+ set_if_respond_to! hash, :system_id # For Entity declarations
267
+ set_if_respond_to! hash, :attribute_type # For attribute declarations
268
+ set_if_respond_to! hash, :default # For attribute declarations
269
+ set_if_respond_to! hash, :enumeration # For attribute declarations
270
+ set_if_respond_to! hash, :element_type # For element declarations
271
+ set_if_respond_to! hash, :prefix # For element declarations
272
+
273
+ set_content! hash, [Node::DOCUMENT_NODE, Node::ELEMENT_NODE]
274
+ set_attributes! hash
275
+ set_if_respond_to! hash, :line
276
+ if respond_to? :namespace and namespace
277
+ hash[:namespace] = namespace.to_hash
145
278
  end
279
+
280
+ # Recurse into children
281
+ add_children! self, hash, [Nokogiri::XML::Node::DTD_NODE, Node::ATTRIBUTE_NODE]
146
282
  hash
147
283
  end
148
284
  end
@@ -1,18 +1,20 @@
1
1
  # coding: utf-8
2
2
  lib = File.expand_path('../lib', __FILE__)
3
3
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
- require 'xml/to/hash/version'
4
+ require 'xml/to/hash'
5
5
 
6
6
  Gem::Specification.new do |spec|
7
7
  spec.name = 'xml-to-hash'
8
8
  spec.version = Xml::To::Hash::VERSION
9
9
  spec.authors = ['Maarten Trompper']
10
- spec.email = ['m.f.a.trompper@uva.nl']
10
+ spec.email = ['maartentrompper@gmail.com']
11
11
 
12
- spec.summary = %q{Transparently convert XML documents to Ruby hashes (...and JSON, and beyond)}
13
- spec.description = %q{This gem add a to_hash method to Nokogiri XML nodes into a Ruby hash. We generate a hash,
14
- in which all keys are constants.
15
- NOTE: This gem ignores fancy stuff like doctypes and entity declarations.}
12
+ spec.summary = %q{Transparently convert XML documents to Ruby hashes (and JSON, and beyond)}
13
+ spec.description = %q{This gem add a `to_hash` method to Nokogiri XML nodes into a Ruby hash. In the resulting hash, all keys are constants.
14
+
15
+ This gem also picks up attributes, processing instructions and doctype declarations. The resulting Hash is wordy, but complete.
16
+
17
+ As an added bonus, we include line numbers where possible.}
16
18
  spec.homepage = 'https://github.com/digitalheir/ruby-xml-to-hash'
17
19
  spec.license = 'MIT'
18
20
 
@@ -33,5 +35,5 @@ NOTE: This gem ignores fancy stuff like doctypes and entity declarations.}
33
35
  spec.add_development_dependency 'rake', '~> 10.0'
34
36
  spec.add_development_dependency 'rspec', '~> 2.4'
35
37
 
36
- spec.add_runtime_dependency 'nokogiri', '~> 1.6.6.2', '>= 1.6.6.2'
38
+ spec.add_runtime_dependency 'nokogiri', '~> 1', '>= 1.6'
37
39
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: xml-to-hash
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Maarten Trompper
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-06-12 00:00:00.000000000 Z
11
+ date: 2015-06-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -58,25 +58,28 @@ dependencies:
58
58
  requirements:
59
59
  - - "~>"
60
60
  - !ruby/object:Gem::Version
61
- version: 1.6.6.2
61
+ version: '1'
62
62
  - - ">="
63
63
  - !ruby/object:Gem::Version
64
- version: 1.6.6.2
64
+ version: '1.6'
65
65
  type: :runtime
66
66
  prerelease: false
67
67
  version_requirements: !ruby/object:Gem::Requirement
68
68
  requirements:
69
69
  - - "~>"
70
70
  - !ruby/object:Gem::Version
71
- version: 1.6.6.2
71
+ version: '1'
72
72
  - - ">="
73
73
  - !ruby/object:Gem::Version
74
- version: 1.6.6.2
75
- description: "This gem add a to_hash method to Nokogiri XML nodes into a Ruby hash.
76
- We generate a hash, \nin which all keys are constants. \nNOTE: This gem ignores
77
- fancy stuff like doctypes and entity declarations."
74
+ version: '1.6'
75
+ description: |-
76
+ This gem add a `to_hash` method to Nokogiri XML nodes into a Ruby hash. In the resulting hash, all keys are constants.
77
+
78
+ This gem also picks up attributes, processing instructions and doctype declarations. The resulting Hash is wordy, but complete.
79
+
80
+ As an added bonus, we include line numbers where possible.
78
81
  email:
79
- - m.f.a.trompper@uva.nl
82
+ - maartentrompper@gmail.com
80
83
  executables: []
81
84
  extensions: []
82
85
  extra_rdoc_files: []
@@ -84,15 +87,14 @@ files:
84
87
  - ".gitignore"
85
88
  - ".rspec"
86
89
  - ".travis.yml"
87
- - CODE_OF_CONDUCT.md
90
+ - CODE_OF_CONDUCT
88
91
  - Gemfile
89
- - LICENSE.txt
90
- - README.md
92
+ - LICENSE
93
+ - README
91
94
  - Rakefile
92
95
  - bin/console
93
96
  - bin/setup
94
97
  - lib/xml/to/hash.rb
95
- - lib/xml/to/hash/version.rb
96
98
  - xml-to-hash.gemspec
97
99
  homepage: https://github.com/digitalheir/ruby-xml-to-hash
98
100
  licenses:
@@ -117,5 +119,5 @@ rubyforge_project:
117
119
  rubygems_version: 2.4.7
118
120
  signing_key:
119
121
  specification_version: 4
120
- summary: Transparently convert XML documents to Ruby hashes (...and JSON, and beyond)
122
+ summary: Transparently convert XML documents to Ruby hashes (and JSON, and beyond)
121
123
  test_files: []
data/README.md DELETED
@@ -1,159 +0,0 @@
1
- # XML to Hash
2
-
3
- Ruby gem to convert XML into Hash (and into JSON).
4
-
5
- ## Installation
6
-
7
- Add this line to your application's Gemfile:
8
-
9
- ```ruby
10
- gem 'xml-to-hash'
11
- ```
12
-
13
- And then execute:
14
-
15
- $ bundle
16
-
17
- Or install it yourself as:
18
-
19
- $ gem install xml-to-hash
20
-
21
- ## Usage
22
-
23
- ```ruby
24
- include 'xml/to/hash'
25
-
26
- xml_string = STR_XML = <<-EOS
27
- <?xml version="1.0" encoding="UTF-8" ?>
28
- <!DOCTYPE author [
29
- <!ELEMENT author (#PCDATA)>
30
- <!ENTITY MyParamEntity "Has been expanded">
31
- <!ENTITY js "Me">
32
- ]>
33
- <myRoot>
34
- some text
35
- <!--
36
- In comments we can use ]]>
37
- <
38
- &, ', and ", but %MyParamEntity; will not be expanded-->
39
- <![CDATA[
40
- Character Data block <!-- <, & ' " --> *and* %MyParamEntity;
41
- ]]>
42
- <?linebreak?>
43
- <deeper xmlns="lol://some-namespace" how-deep="very-deep">randomtext
44
- <even
45
- lol:my-attr="just an attribute"
46
- xmlns:lol=\'lol://my.name.space/\' deeper="true">O</even></deeper>
47
- </myRoot>
48
- EOS
49
-
50
- xml = Nokogiri::XML STR_XML
51
- hash = xml.to_hash
52
-
53
- puts JSON.pretty_generate(hash)
54
- ```
55
-
56
- produces
57
-
58
- ```json
59
- {
60
- "type": "element",
61
- "name": "myRoot",
62
- "children": [
63
- {
64
- "type": "text",
65
- "content": "\n some text\n "
66
- },
67
- {
68
- "type": "comment",
69
- "content": "\n In comments we can use ]]>\n <\n &, ', and \", but %MyParamEntity; will not be expanded"
70
- },
71
- {
72
- "type": "text",
73
- "content": "\n "
74
- },
75
- {
76
- "type": "cdata",
77
- "content": "\n Character Data block <!-- <, & ' \" --> *and* %MyParamEntity; \n "
78
- },
79
- {
80
- "type": "text",
81
- "content": "\n "
82
- },
83
- {
84
- "type": "pi",
85
- "name": "linebreak"
86
- },
87
- {
88
- "type": "text",
89
- "content": "\n "
90
- },
91
- {
92
- "type": "element",
93
- "attrs": [
94
- {
95
- "name": "how-deep",
96
- "value": "very-deep"
97
- }
98
- ],
99
- "name": "deeper",
100
- "namespace": {
101
- "href": "lol://some-namespace"
102
- },
103
- "children": [
104
- {
105
- "type": "text",
106
- "content": "randomtext\n "
107
- },
108
- {
109
- "type": "element",
110
- "attrs": [
111
- {
112
- "name": "my-attr",
113
- "value": "just an attribute",
114
- "namespace": {
115
- "href": "lol://my.name.space/",
116
- "prefix": "lol"
117
- }
118
- },
119
- {
120
- "name": "deeper",
121
- "value": "true"
122
- }
123
- ],
124
- "name": "even",
125
- "namespace": {
126
- "href": "lol://some-namespace"
127
- },
128
- "children": [
129
- {
130
- "type": "text",
131
- "content": "O"
132
- }
133
- ]
134
- }
135
- ]
136
- },
137
- {
138
- "type": "text",
139
- "content": " \n"
140
- }
141
- ]
142
- }
143
- ```
144
-
145
- ## Development
146
-
147
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake rspec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
148
-
149
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
150
-
151
- ## Contributing
152
-
153
- Bug reports and pull requests are welcome on GitHub at https://github.com/digitalheir/xml-to-hash. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
154
-
155
-
156
- ## License
157
-
158
- The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
159
-
@@ -1,7 +0,0 @@
1
- module Xml
2
- module To
3
- module Hash
4
- VERSION = '0.9.0'
5
- end
6
- end
7
- end