xml_parser 0.2.1 → 0.3.0
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 +4 -4
- data/README.md +3 -0
- data/lib/xml_parser/document.rb +71 -4
- data/lib/xml_parser/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4bff8dc1fc6aef722fde275bb33821af85f364be
|
4
|
+
data.tar.gz: 94367da076ea09440a81d80b9806b6da29da01b9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 13569866bdb09f714dce4bdc8852cc5ffdb0a2166092e741d789ff0bd9654ba3d4f8a408d97cd0f294d4650d1c6ed3a442ea47e540a24c99f804582e30b6a290
|
7
|
+
data.tar.gz: 400e0fe0cf0b7637db3ec822d6409249ae75604a549733b65463e698965d232638863326a251ab579ae88d4e9fc63fab7462bcc6b75eac4bb00be44c68a60652
|
data/README.md
CHANGED
@@ -38,6 +38,9 @@ doc = XmlParser::Document.new("<sample><elm>sample xml</elm></sample>")
|
|
38
38
|
match_values = doc.xpath_map("/sample/test/@name") { |elm|
|
39
39
|
puts elm
|
40
40
|
}
|
41
|
+
|
42
|
+
# parse xml from yaml file
|
43
|
+
doc = XmlParser::Document.from_yml("/var/home/hoge.yml", "root_element_name", "default_attr_name")
|
41
44
|
```
|
42
45
|
|
43
46
|
## Development
|
data/lib/xml_parser/document.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
require "rexml/document"
|
2
|
+
require "yaml"
|
2
3
|
require "pp"
|
3
4
|
|
4
5
|
# XML Parser module
|
@@ -8,10 +9,18 @@ module XmlParser
|
|
8
9
|
class Document
|
9
10
|
|
10
11
|
# constructor.
|
11
|
-
# @param [String]
|
12
|
-
def initialize(
|
12
|
+
# @param [String / REXML::Document] xml xml string / REXML::Document object.
|
13
|
+
def initialize(xml)
|
13
14
|
|
14
|
-
|
15
|
+
# xml string
|
16
|
+
if xml.is_a?(String)
|
17
|
+
@doc = REXML::Document.new(xml)
|
18
|
+
# REXML::Document object
|
19
|
+
elsif xml.is_a?(REXML::Document)
|
20
|
+
@doc = xml
|
21
|
+
else
|
22
|
+
raise TypeError.new("XmlParser::Document initialize error.")
|
23
|
+
end
|
15
24
|
end
|
16
25
|
|
17
26
|
# if xpath match, do map process.
|
@@ -29,7 +38,7 @@ module XmlParser
|
|
29
38
|
}
|
30
39
|
end
|
31
40
|
|
32
|
-
# if xpath
|
41
|
+
# if xpath match, do each process.
|
33
42
|
# if block_given? == false, display each element by pp. Use confirm xpath.
|
34
43
|
# @param [String] xpath xpath
|
35
44
|
def xpath_each(xpath)
|
@@ -64,5 +73,63 @@ module XmlParser
|
|
64
73
|
xml_str = File.read(filepath)
|
65
74
|
XmlParser::Document.new(xml_str)
|
66
75
|
end
|
76
|
+
|
77
|
+
# initialize from yml file.
|
78
|
+
# @param [String] filepath read yml filepath. absolute.
|
79
|
+
# @param [String] root_name root element name
|
80
|
+
# @param [String] attr_name attribute name
|
81
|
+
# @return [XmlParser::Document] parser.
|
82
|
+
def self.from_yml(filepath, root_name="root", attr_name="value")
|
83
|
+
|
84
|
+
# load yml file.
|
85
|
+
yml = YAML.load_file(filepath)
|
86
|
+
# parse to xml.
|
87
|
+
parse_xml_from_hash(yml, root_name, attr_name)
|
88
|
+
end
|
89
|
+
|
90
|
+
private
|
91
|
+
|
92
|
+
# parse hash to xml.
|
93
|
+
# @param [Hash] hash hash
|
94
|
+
# @param [String] root_name root element name
|
95
|
+
# @param [String] attr_name attribute name
|
96
|
+
# @return [XmlParser::Document] document
|
97
|
+
def self.parse_xml_from_hash(hash, root_name, attr_name)
|
98
|
+
|
99
|
+
doc = REXML::Document.new("<#{root_name}/>")
|
100
|
+
|
101
|
+
# create under root element.
|
102
|
+
hash.each { |key, value|
|
103
|
+
elm = parse_element_from_hash(key, value, attr_name)
|
104
|
+
doc.root.add(elm)
|
105
|
+
}
|
106
|
+
|
107
|
+
XmlParser::Document.new(doc)
|
108
|
+
end
|
109
|
+
|
110
|
+
# parse element from yml hash.
|
111
|
+
# @param [String] key key, set element name.
|
112
|
+
# @param [String / Hash] value if value is String, set value attribute. but value is Hash, call this method.
|
113
|
+
# @param [String] attr_name attribute name
|
114
|
+
# @return [REXML::Element] element
|
115
|
+
def self.parse_element_from_hash(key, value, attr_name)
|
116
|
+
|
117
|
+
elm = REXML::Element.new(key)
|
118
|
+
|
119
|
+
# if value is String
|
120
|
+
if value.is_a?(String)
|
121
|
+
# set element. default attribute name = value.
|
122
|
+
elm.add_attribute(attr_name, value)
|
123
|
+
# if value is Hash
|
124
|
+
elsif value.is_a?(Hash)
|
125
|
+
# create child element.
|
126
|
+
value.each { |c_key, c_value|
|
127
|
+
c_elm = parse_element_from_hash(c_key, c_value, attr_name)
|
128
|
+
elm.add(c_elm)
|
129
|
+
}
|
130
|
+
end
|
131
|
+
|
132
|
+
elm
|
133
|
+
end
|
67
134
|
end
|
68
135
|
end
|
data/lib/xml_parser/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: xml_parser
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- h.shigemoto
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-08-
|
11
|
+
date: 2016-08-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|