-
Notifications
You must be signed in to change notification settings - Fork 0
/
xml_parsing.rb
83 lines (72 loc) · 1.66 KB
/
xml_parsing.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
require 'benchmark/ips'
require 'nokogiri'
require 'ox'
require 'oga'
require 'rexml/document'
xml = DATA.read
ox = Ox.parse(xml)
nokogiri = Nokogiri::XML(xml)
oga = Oga.parse_xml(xml)
rex = REXML::Document.new xml
Benchmark.ips do |x|
x.report('Nokogiri: NodeSet') do
map = []
nokogiri.css('record').each do |node|
map << node.children
end
map
map.each do |node|
node.css('attr1').inner_text
node.css('attr2').inner_text
node.css('attr3').inner_text
node.css('attr4').inner_text
end
end
x.report('Nokogiri: Element') do
nokogiri.css('record').each do |node|
node.css('attr1').inner_text
node.css('attr2').inner_text
node.css('attr3').inner_text
node.css('attr4').inner_text
end
end
x.report('OX') do
ox.locate('record').each do |node|
node.locate('attr1').first.text
node.locate('attr2').first.text
node.locate('attr3').first.text
node.locate('attr4').first.text
end
end
x.report('OGA') do
oga.xpath('//record') do |node|
node.at_xpath('attr1').text
node.at_xpath('attr2').text
node.at_xpath('attr3').text
node.at_xpath('attr4').text
end
end
x.report('REXML') do
rex.elements.each('*/record') do |node|
node.elements['attr1'].text
node.elements['attr2'].text
node.elements['attr3'].text
node.elements['attr4'].text
end
end
end
__END__
<root>
<record>
<attr1>bla1</attr1>
<attr2>bla2</attr2>
<attr3>bla3</attr3>
<attr4>bla4</attr4>
</record>
<record>
<attr1>bla1</attr1>
<attr2>bla2</attr2>
<attr3>bla3</attr3>
<attr4>bla4</attr4>
</record>
</root>