class Entityjs::ParseXML

Public Class Methods

parse(contents) click to toggle source
# File lib/entityjs/parsers/parse_xml.rb, line 27
def self.parse(contents)
  if contents.nil? || contents.empty?
    return '{}'
  end
  
  if contents.is_a? String
    contents = self.parse_to_hash(contents)
  end
  
  #to string
  contents = contents.to_json
  
  #remove @
  contents = contents.gsub('"@','"')
  
  
  #transform string-numbers into numbers
  
  return contents.gsub(/"[0-9\.]*"/){|s| s[1..-2] }
end
parse_to_hash(contents) click to toggle source
# File lib/entityjs/parsers/parse_xml.rb, line 5
def self.parse_to_hash(contents)
  if contents.nil? || contents.empty?
    return '{}'
  end
  #might need different transfomration
  #remove header
  
  contents = contents.gsub(/<\?xml.*\?>/, '')
  #convert to hash
  contents = CobraVsMongoose.xml_to_hash(contents)
  
  #remove root
  contents.each do |i,v|
    if !v.nil?
      contents = v
      break
    end
  end
  
  return contents
end