module Ruspider::XmlUtils

XML utilities

Public Instance Methods

array_from_xml(xml_str, obj_name, field) click to toggle source
# File lib/ruspider/xml_utils.rb, line 34
def array_from_xml(xml_str, obj_name, field)
  xml = get_nodes(Nokogiri::XML(xml_str), obj_name)
  arr = xml.xpath("//cs:#{field}", 'cs' => 'http://www.chemspider.com/')
  arr.map(&:content)
end
get_first_node(xml, node_name) click to toggle source
# File lib/ruspider/xml_utils.rb, line 15
def get_first_node(xml, node_name)
  get_nodes(xml, node_name).first
end
get_nodes(xml, node_name) click to toggle source
# File lib/ruspider/xml_utils.rb, line 8
def get_nodes(xml, node_name)
  xml.xpath(
    "//cs:#{node_name}",
    'cs' => 'http://www.chemspider.com/'
  )
end
hash_from_string(xml_str, obj_name, field) click to toggle source
# File lib/ruspider/xml_utils.rb, line 29
def hash_from_string(xml_str, obj_name, field)
  xml = get_first_node(Nokogiri::XML(xml_str), obj_name)
  hash_from_xml(xml, field)
end
hash_from_xml(xml, field) click to toggle source
# File lib/ruspider/xml_utils.rb, line 19
def hash_from_xml(xml, field)
  hash = {}
  field.each do |k, v|
    node = xml.xpath("//cs:#{k}", 'cs' => 'http://www.chemspider.com/')
    hash[v.to_sym] = node.first.content
  end

  hash
end