libxml-to-hash 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/libxml_to_hash.rb +138 -0
- metadata +61 -0
@@ -0,0 +1,138 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
|
3
|
+
# USAGE: Hash.from_libxml(YOUR_XML_STRING)
|
4
|
+
require 'xml/libxml'
|
5
|
+
# adapted from
|
6
|
+
# http://movesonrails.com/articles/2008/02/25/libxml-for-active-resource-2-0
|
7
|
+
|
8
|
+
# This class is required to represent Nodes that contain
|
9
|
+
# attributes as well as other content, for example:
|
10
|
+
# <karin zak="lebt">schon</karin>
|
11
|
+
# We cannot non-ambigiously represent this node as a hash
|
12
|
+
# because Hashes may contain arbitrary keys.
|
13
|
+
# Having a separate class for representing such nodes
|
14
|
+
# allows for easy testing for the class (which we have
|
15
|
+
# to do anyway since hash values may be Strings, Arrays
|
16
|
+
# or other Hashes already)
|
17
|
+
# Update: No need for testing when using the iterable method.
|
18
|
+
# Usage: LibXmlNode.new("karin", "zak")
|
19
|
+
|
20
|
+
class LibXmlNode < Object
|
21
|
+
attr_accessor :subnodes
|
22
|
+
attr_accessor :attributes
|
23
|
+
attr_accessor :text
|
24
|
+
|
25
|
+
def initialize
|
26
|
+
@subnodes = {}
|
27
|
+
@attributes = {}
|
28
|
+
@text = ""
|
29
|
+
end
|
30
|
+
|
31
|
+
def self.create(n, a, t)
|
32
|
+
l = LibXmlNode.new
|
33
|
+
l.subnodes = n
|
34
|
+
l.attributes = a
|
35
|
+
l.text = t
|
36
|
+
l
|
37
|
+
end
|
38
|
+
|
39
|
+
def add_attribute(key, value)
|
40
|
+
@attributes[key] = value
|
41
|
+
end
|
42
|
+
|
43
|
+
def add_node(key, value)
|
44
|
+
if @subnodes[key]
|
45
|
+
if @subnodes[key].is_a? Object::Array
|
46
|
+
@subnodes[key] << value
|
47
|
+
else
|
48
|
+
@subnodes[key] = [@subnodes[key], value]
|
49
|
+
end
|
50
|
+
else
|
51
|
+
@subnodes[key] = value
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
def add_text(t)
|
56
|
+
@text << t
|
57
|
+
end
|
58
|
+
|
59
|
+
def simplify
|
60
|
+
if @attributes.empty?
|
61
|
+
if @text == ""
|
62
|
+
return @subnodes
|
63
|
+
end
|
64
|
+
if @subnodes == {}
|
65
|
+
return @text
|
66
|
+
end
|
67
|
+
end
|
68
|
+
return self
|
69
|
+
end
|
70
|
+
|
71
|
+
def ==(other)
|
72
|
+
if other.class == LibXmlNode
|
73
|
+
if @subnodes == other.subnodes and @attributes == other.attributes and @text == other.text
|
74
|
+
return true
|
75
|
+
end
|
76
|
+
end
|
77
|
+
false
|
78
|
+
end
|
79
|
+
|
80
|
+
def iterable
|
81
|
+
[self]
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
class Hash
|
86
|
+
def iterable
|
87
|
+
[self]
|
88
|
+
end
|
89
|
+
|
90
|
+
class << self
|
91
|
+
def from_libxml!(xml, strict=true)
|
92
|
+
XML.default_load_external_dtd = false
|
93
|
+
XML.default_pedantic_parser = strict
|
94
|
+
result = XML::Parser.string(xml).parse
|
95
|
+
return { result.root.name.to_s => xml_node_to_hash(result.root)}
|
96
|
+
end
|
97
|
+
|
98
|
+
def from_libxml(xml, strict=true)
|
99
|
+
begin
|
100
|
+
from_libxml!(xml, strict)
|
101
|
+
rescue Exception => e
|
102
|
+
nil
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
def xml_node_to_hash(node)
|
107
|
+
n = LibXmlNode.new
|
108
|
+
if node.element?
|
109
|
+
node.attributes.each do |attribute|
|
110
|
+
n.add_attribute attribute.name.to_s, attribute.value
|
111
|
+
end
|
112
|
+
|
113
|
+
node.each_child do |child|
|
114
|
+
if child.text?
|
115
|
+
if not child.children?
|
116
|
+
n.add_text child.content.to_s.strip
|
117
|
+
end
|
118
|
+
else
|
119
|
+
n.add_node child.name.to_s, xml_node_to_hash(child)
|
120
|
+
end
|
121
|
+
end
|
122
|
+
end
|
123
|
+
return n.simplify
|
124
|
+
end
|
125
|
+
end
|
126
|
+
end
|
127
|
+
|
128
|
+
class String
|
129
|
+
def iterable
|
130
|
+
[self]
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
134
|
+
class Array
|
135
|
+
def iterable
|
136
|
+
self
|
137
|
+
end
|
138
|
+
end
|
metadata
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: libxml-to-hash
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Johannes Thoma
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-06-05 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: libxml-ruby
|
16
|
+
requirement: &79542540 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *79542540
|
25
|
+
description: ! 'A simple library to convert XML strings to hashes using libxml
|
26
|
+
|
27
|
+
|
28
|
+
This adds to from_lib_xml method to the Hash class which uses libxml (which
|
29
|
+
|
30
|
+
is much faster than ReXML) to convert a XML string into a Hash'
|
31
|
+
email: johannes.thoma@gmx.at
|
32
|
+
executables: []
|
33
|
+
extensions: []
|
34
|
+
extra_rdoc_files: []
|
35
|
+
files:
|
36
|
+
- lib/libxml_to_hash.rb
|
37
|
+
homepage: http://rubygems.org/gems/libxml-to-hash
|
38
|
+
licenses: []
|
39
|
+
post_install_message:
|
40
|
+
rdoc_options: []
|
41
|
+
require_paths:
|
42
|
+
- lib
|
43
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
44
|
+
none: false
|
45
|
+
requirements:
|
46
|
+
- - ! '>='
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '0'
|
49
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
requirements: []
|
56
|
+
rubyforge_project:
|
57
|
+
rubygems_version: 1.8.10
|
58
|
+
signing_key:
|
59
|
+
specification_version: 3
|
60
|
+
summary: A simple library to convert XML strings to hashes using libxml
|
61
|
+
test_files: []
|