class KXML::Document
Attributes
encoding[RW]
root[RW]
version[RW]
Public Class Methods
new(data) { |root| ... }
click to toggle source
# File lib/xml/kxml.rb, line 7 def initialize(data) case data when KXML::Node @root = data when String if data =~ /^[<>\/]/ parse_from_string(data) else @root = Node.new(data) end when File parse_from_file(data) else raise "Unsupport Param Type : #{data.class}" end yield @root if block_given? end
Public Instance Methods
save_to(file_path)
click to toggle source
# File lib/xml/kxml.rb, line 25 def save_to(file_path) File.open(file_path,"w+"){|f| f.write to_s } end
to_s(format = Format::PRETTY)
click to toggle source
# File lib/xml/kxml.rb, line 31 def to_s(format = Format::PRETTY) return @root.to_s(format) end
Private Instance Methods
parse_from_file(file)
click to toggle source
# File lib/xml/kxml.rb, line 43 def parse_from_file(file) pre_parse() line = file.gets while(line != nil) push_data(line) line = file.gets end post_parse() end
parse_from_string(data)
click to toggle source
# File lib/xml/kxml.rb, line 37 def parse_from_string(data) pre_parse() push_data(data) post_parse() end
post_parse()
click to toggle source
# File lib/xml/kxml.rb, line 96 def post_parse @__parser.finish @__parser = nil @__tag_stack = nil end
pre_parse()
click to toggle source
# File lib/xml/kxml.rb, line 53 def pre_parse require File.expand_path('../xml_parser.rb',__FILE__) @__parser = SAXParser.new @__tag_stack = [] @__parser.handler=->(param){ case param[:type] when SAXParser::DOCUMENT_STATEMENT @encoding = param[:attributes]['encoding'] @version = param[:attributes]['version'] when SAXParser::START_TAG node = Node.new(param[:name]) node.attributes.merge!(param[:attributes]) if param[:attributes] != nil if @root == nil @root = node else @__tag_stack.last << node end @__tag_stack << node when SAXParser::END_TAG raise "Tag mismatch : <#{@__tag_stack.last.name}> - </#{param[:name]}>" if @__tag_stack.last ==nil || @__tag_stack.last.name != param[:name] @__tag_stack.pop when SAXParser::SELF_CLOSE_TAG node = Node.new(param[:name]) node.attributes.merge!(param[:attributes]) if param[:attributes] != nil if @root == nil @root = node else @__tag_stack.last << node end when SAXParser::COMMENTS #ignore when SAXParser::CONTENT next if @__tag_stack.last == nil @__tag_stack.last.content = '' if @__tag_stack.last.content == nil @__tag_stack.last.content += param[:name].gsub(/\s/,"") end } end
push_data(data)
click to toggle source
# File lib/xml/kxml.rb, line 92 def push_data(data) @__parser << data end