Sha256: c32d52d4093bf7f49682fbdc6d7c3b3926f9602fe07729c48ed3054457883da9

Contents?: true

Size: 789 Bytes

Versions: 4

Compression:

Stored size: 789 Bytes

Contents

# Convenience methods for libxml classes.
module XML
  # Find first element matching +xpath+ and return its content
  # (intended for use as a mixed-in method for Document/Node instances).
  # 
  # +xpath+: XPath query to run against self.
  # 
  # Returns nil if no element matches +xpath+.
  def xget(xpath)
    nodes = self.find(xpath).to_a
    if nodes.empty?
      return nil
    else
      return nodes.first.content
    end
  end

  # Parse an XML string into an XML::Document instance.
  def self.get_xml_doc(xml_str)
    parser = XML::Parser.new
    parser.string = xml_str
    parser.parse
  end

  # Make custom methods accessible to Document class.
  class Document
    include XML
  end

  # Make custom methods accessible to Node class.
  class Node
    include XML
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
s33r-0.5.3 lib/s33r/libxml_extensions.rb
s33r-0.5.2 lib/s33r/libxml_extensions.rb
s33r-0.5.4 lib/s33r/libxml_extensions.rb
s33r-0.5.5 lib/s33r/libxml_extensions.rb