xml_parser 0.1.0 → 0.2.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: 6a579f18b6d3b17991aa7634bb4522de76da2dd7
4
- data.tar.gz: 0e5aaf35a63fe25e8599f7f6914e6fb93efdfa74
3
+ metadata.gz: a95b2b27cfa5f5ab70441e1a2b7ab0b7cd7c6bee
4
+ data.tar.gz: 0b1a426678e054a71e0635ca88760001492bde05
5
5
  SHA512:
6
- metadata.gz: 099f2df702b5fdb64956b5aaf6cf9d5f01a2952fa97c941461cfabec147311c891a7b20a6dccb58e0437c47f558055d34200ff60d6ff643ad51242541dbe56f3
7
- data.tar.gz: 594cde00f78f50affd0b0b6ca972f56c5142dac0ce3d0bbc7d3c7d45f6e51ba733b5a606e44f67ef959d9805722d0490e31734f44a247d0a0f458220a3a6492d
6
+ metadata.gz: 945b5b59de0c90d40f324e4b4cf502bb47ae677bea17e68a8fdcf78fa97c9e47dfaf40e826d704e9a31e9ab60e5af9fd97c9db83cde9788baf9ab1f65b3b23bc
7
+ data.tar.gz: 9b64de0d372f70d3afa275e3c97e83daed57944817afd6c3fbf432c874f837b84082fd67f26c7c0cbc0f1d9d5f049af1d4db27c1def07e8e54e482d2467973e0
data/README.md CHANGED
@@ -30,6 +30,14 @@ str = XmlParser::Text.multi_normalize(["a > b; < c", "a > b; < c"])
30
30
  # unnormalize
31
31
  str = XmlParser::Text.unnormalize("a &gt; b; &lt; c")
32
32
  str = XmlParser::Text.multi_unnormalize(["a &gt; b; &lt; c", "a &gt; b; &lt; c"])
33
+
34
+ # read xml
35
+ doc = XmlParser::Document.new("<sample><elm>sample xml</elm></sample>")
36
+ # you can read from file.
37
+ # doc = XmlParser::Document.from_file("/var/sample.xml")
38
+ match_values = doc.xpath_map("/sample/test/@name") { |elm|
39
+ puts elm
40
+ }
33
41
  ```
34
42
 
35
43
  ## Development
@@ -0,0 +1,55 @@
1
+ require "rexml/document"
2
+ require "pp"
3
+
4
+ # XML Parser module
5
+ module XmlParser
6
+
7
+ # Document Class
8
+ class Document
9
+
10
+ # constructor.
11
+ # @param [String] xml_str xml string.
12
+ def initialize(xml_str)
13
+ @doc = REXML::Document.new(xml_str)
14
+ end
15
+
16
+ # if xpath match, do map process.
17
+ # if block_given? == false, return xpath match values.
18
+ # @param [String] xpath xpath
19
+ # @return [Array] match value by Array.
20
+ def xpath_map(xpath)
21
+
22
+ matches = REXML::XPath.match(@doc, xpath)
23
+ return matches unless block_given?
24
+
25
+ # if block_given, do map process.
26
+ matches.map { |elm|
27
+ yield elm
28
+ }
29
+ end
30
+
31
+ # if xpath math, do each process.
32
+ # if block_given? == false, display each element by pp. Use confirm xpath.
33
+ # @param [String] xpath xpath
34
+ def xpath_each(xpath)
35
+
36
+ REXML::XPath.match(@doc, xpath).each { |elm|
37
+ if block_given?
38
+ yield elm
39
+ else
40
+ pp elm
41
+ end
42
+ }
43
+ end
44
+
45
+ # initialize from File.
46
+ # @param [String] filepath read fileptah. absolute.
47
+ # @return [XmlParser::Document] parser.
48
+ def self.from_file(filepath)
49
+
50
+ # read file contents.
51
+ xml_str = File.read(filepath)
52
+ XmlParser::Document.new(xml_str)
53
+ end
54
+ end
55
+ end
@@ -1,3 +1,3 @@
1
1
  module XmlParser
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
data/lib/xml_parser.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require "xml_parser/version"
2
+ require "xml_parser/document"
2
3
  require "xml_parser/text"
3
4
 
4
5
  module XmlParser
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.1.0
4
+ version: 0.2.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-19 00:00:00.000000000 Z
11
+ date: 2016-08-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -70,6 +70,7 @@ files:
70
70
  - bin/console
71
71
  - bin/setup
72
72
  - lib/xml_parser.rb
73
+ - lib/xml_parser/document.rb
73
74
  - lib/xml_parser/text.rb
74
75
  - lib/xml_parser/version.rb
75
76
  - xml_parser.gemspec