Created
October 3, 2012 08:00
-
-
Save shitsyndrome/3825717 to your computer and use it in GitHub Desktop.
sucksssssssssssssssssssss!!!!!!!!!!!!!!!!!!!!!!!!!!!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require_relative "untangle/version" | |
| require "nokogiri" | |
| require "stringio" | |
| module Untangle | |
| class Element | |
| attr_accessor :is_root | |
| def initialize(name=nil, attributes=nil) | |
| @name = name | |
| @attributes = attributes | |
| @children = [] | |
| @is_root = false | |
| @cdata = '' | |
| end | |
| def add_child element | |
| @children << element | |
| end | |
| def add_cdata cdata | |
| @cdata = @cdata + cdata | |
| end | |
| def get_attriute key | |
| @attributes.fetch(key) | |
| end | |
| def get_elements name=nil | |
| if name | |
| @children.select{|e| e.name == name } | |
| else | |
| @children | |
| end | |
| end | |
| def get key | |
| get_attriute key | |
| end | |
| def [] key | |
| get_attriute key | |
| end | |
| def to_s | |
| "Element <#{@name}> with attributes #{@attributes} and children #{@children}" | |
| end | |
| end | |
| class Handler < Nokogiri::XML::SAX::Document | |
| attr_accessor :parser, :parse_file | |
| def initialize | |
| @root = Element.new(nil, nil) | |
| @root.is_root = true | |
| @elements = [] | |
| end | |
| def start_element(name, attributes) | |
| name = name.gsub('-', '_').gsub('.', '_').gsub(':', '_') | |
| attrs = {} | |
| attributes.each {|k, v| attrs[k] = v } | |
| element = Element.new(name, attrs) | |
| if @elements.size > 0 | |
| @elements.last.add_child element | |
| else | |
| @root.add_child element | |
| end | |
| @elements << element | |
| end | |
| def end_element name | |
| @elements.pop | |
| end | |
| def characters cdata | |
| @elements.last.add_cdata cdata | |
| end | |
| end | |
| def parsing fname | |
| if fname == nil || fname.strip == '' | |
| raise | |
| end | |
| # parser | |
| parser = Nokogiri::XML::SAX::Parser.new(Handler.new) | |
| # parser = Nokogiri::XML::SAX::Parser.new(Nokogiri::XML::SAX::Document.new) | |
| if File.exists?(fname) || is_url?(fname) | |
| parser.parse_file fname | |
| else | |
| parser.instance_eval { | |
| p instance_variables | |
| p document | |
| }.parse_file(StringIO.new(fname)) | |
| end | |
| end | |
| def is_url? str | |
| str.start_with?('http://') || str.start_with?('https://') | |
| end | |
| end | |
| if __FILE__ == $0 | |
| include Untangle | |
| o = Untangle.parsing('<node id="5"><subnode value="abc"/></node>') | |
| p o | |
| p "Node id = %s, subnode value = %s" % [o.node['id'], o.node.subnode['value']] | |
| end | |
| # $ ruby untangle.rb | |
| # [:@encoding, :@document, :@warned] | |
| # #<Untangle::Handler:0x8ea3b18 @root=Element <> with attributes and children [], @elements=[]> | |
| # untangle.rb:92:in `parsing': wrong number of arguments (1 for 0) (ArgumentError) | |
| # from untangle.rb:107:in `<main>' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment