mysql-xml 0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/Changelog +5 -0
- data/LICENSE +14 -0
- data/README.rdoc +12 -0
- data/lib/mysql-xml.rb +63 -0
- metadata +58 -0
data/Changelog
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
This file is part of mysql-xml
|
2
|
+
|
3
|
+
mysql-xml is free software: you can redistribute it and/or modify
|
4
|
+
it under the terms of the GNU General Public License as published by
|
5
|
+
the Free Software Foundation, either version 3 of the License, or
|
6
|
+
(at your option) any later version.
|
7
|
+
|
8
|
+
mysql-xml is distributed in the hope that it will be useful,
|
9
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
10
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
11
|
+
GNU General Public License for more details.
|
12
|
+
|
13
|
+
You should have received a copy of the GNU General Public License
|
14
|
+
along with mysql-xml. If not, see <http://www.gnu.org/licenses/>.
|
data/README.rdoc
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
#Provides an easy way to parse XML output from mysql -X.
|
2
|
+
#MySQLXML::parse takes an IO object for the XML and a block that gets called
|
3
|
+
#once for each record. The records are represented as hashes keyed by string
|
4
|
+
#representations of column names.
|
5
|
+
#
|
6
|
+
#Example:
|
7
|
+
# MySQLXML::parse(File.new('test.xml')) do |record|
|
8
|
+
# puts record['username']
|
9
|
+
# end
|
10
|
+
#
|
11
|
+
#This uses REXML's stream parser, so it should be reasonably
|
12
|
+
#memory efficient.
|
data/lib/mysql-xml.rb
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
#Provides an easy way to parse XML output from mysql -X.
|
2
|
+
#MySQLXML::parse takes an IO object for the XML and a block that gets called
|
3
|
+
#once for each record.
|
4
|
+
#
|
5
|
+
#Example:
|
6
|
+
# require 'mysql-xml'
|
7
|
+
# MySQLXML::parse(File.new('test.xml')) do |record|
|
8
|
+
# puts record.inspect
|
9
|
+
# end
|
10
|
+
#
|
11
|
+
#This uses REXML's stream parser, so it should be reasonably
|
12
|
+
#memory efficient.
|
13
|
+
require 'rexml/document'
|
14
|
+
require 'rexml/parsers/streamparser'
|
15
|
+
|
16
|
+
module MySQLXML
|
17
|
+
def self.parse(io,&block)
|
18
|
+
listener = MyXListener.new(block)
|
19
|
+
REXML::Document.parse_stream(io,listener)
|
20
|
+
return listener.records
|
21
|
+
end
|
22
|
+
|
23
|
+
class MyXListener
|
24
|
+
attr_reader :records
|
25
|
+
|
26
|
+
def initialize(block)
|
27
|
+
#Track where we are in the tree.
|
28
|
+
@context = []
|
29
|
+
@cur_record = nil
|
30
|
+
@cur_field_name = nil
|
31
|
+
@block = block
|
32
|
+
end
|
33
|
+
|
34
|
+
def tag_start(name, attrs)
|
35
|
+
@context << name
|
36
|
+
if name == 'row'
|
37
|
+
@cur_record = {}
|
38
|
+
elsif name == 'field'
|
39
|
+
@cur_field_name = attrs['name']
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def text(text)
|
44
|
+
if @context[-1] == 'field'
|
45
|
+
@cur_record[@cur_field_name] = text
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def tag_end(name)
|
50
|
+
@context.pop
|
51
|
+
if name == 'field'
|
52
|
+
@cur_field_name = nil
|
53
|
+
elsif name == 'row'
|
54
|
+
@block.call(@cur_record)
|
55
|
+
@cur_record = nil
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
#Silence method missing exceptions since we only
|
60
|
+
#need to handle some events
|
61
|
+
def method_missing(*opts); end
|
62
|
+
end
|
63
|
+
end
|
metadata
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mysql-xml
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: "0.1"
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Andrew Cholakian
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-11-16 00:00:00 -08:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: Provides a simple, fast way of parsing XML dumps of MySQL data.
|
17
|
+
email: andrew@andrewvc.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files: []
|
23
|
+
|
24
|
+
files:
|
25
|
+
- README.rdoc
|
26
|
+
- Changelog
|
27
|
+
- LICENSE
|
28
|
+
- lib/mysql-xml.rb
|
29
|
+
has_rdoc: true
|
30
|
+
homepage: http://www.andrewvc.com/mysql-xml
|
31
|
+
licenses: []
|
32
|
+
|
33
|
+
post_install_message:
|
34
|
+
rdoc_options: []
|
35
|
+
|
36
|
+
require_paths:
|
37
|
+
- lib
|
38
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
39
|
+
requirements:
|
40
|
+
- - ">="
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: "0"
|
43
|
+
version:
|
44
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - ">="
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: "0"
|
49
|
+
version:
|
50
|
+
requirements: []
|
51
|
+
|
52
|
+
rubyforge_project:
|
53
|
+
rubygems_version: 1.3.5
|
54
|
+
signing_key:
|
55
|
+
specification_version: 3
|
56
|
+
summary: Provides a simple, fast way of parsing XML dumps of MySQL data.
|
57
|
+
test_files: []
|
58
|
+
|