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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6d6516f5dde191fe52f58147ee8a8333faea0abd
4
- data.tar.gz: e529124eecaeccb34f653cdab202889f61ac5572
3
+ metadata.gz: 4bff8dc1fc6aef722fde275bb33821af85f364be
4
+ data.tar.gz: 94367da076ea09440a81d80b9806b6da29da01b9
5
5
  SHA512:
6
- metadata.gz: e05aae54a7607a93820e1895feac7d176b5214c32984e7f9c1a3977ead5fc54ee319b964b38c912967f15f90b8038a58bc91262a68f6ff28e9f374c95bdcb2b7
7
- data.tar.gz: bf827d10eb6b6fa9dd89309c89b6ff4f63d800330db63b313adf3cfe2b3b2adfc116efc14055bcd08ac0f1799a4c37a55b98527aceb4e654a47da0fe07e29ff9
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
@@ -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] xml_str xml string.
12
- def initialize(xml_str)
12
+ # @param [String / REXML::Document] xml xml string / REXML::Document object.
13
+ def initialize(xml)
13
14
 
14
- @doc = REXML::Document.new(xml_str)
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 math, do each process.
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
@@ -1,3 +1,3 @@
1
1
  module XmlParser
2
- VERSION = "0.2.1"
2
+ VERSION = "0.3.0"
3
3
  end
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.2.1
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-23 00:00:00.000000000 Z
11
+ date: 2016-08-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler