class CapicuaGen::XMLHelper

Clase ayudante para tratar con archivos XML

Public Class Methods

get_node_from_xml_document(xml, xpath) click to toggle source

obtiene un nodo de un xml

# File lib/CapicuaGen/Tools/xml_helper.rb, line 39
def self.get_node_from_xml_document(xml, xpath)

  # Busco el nodo
  xml_node_xpath= xml.root.xpath(xpath)

  # Si no xml_node_xpath un primer nodo
  return xml_node_xpath.first if xml_node_xpath.first

  return create_node(xml.root, xpath)

end
new() click to toggle source
# File lib/CapicuaGen/Tools/xml_helper.rb, line 32
def initialize

end

Private Class Methods

clear_node_name(node_string) click to toggle source

Limpia un atributo del nombre de un nodo

# File lib/CapicuaGen/Tools/xml_helper.rb, line 80
def self.clear_node_name(node_string)
  if node_string=~/([^\[]+)\[/
    return $1
  else
    return node_string
  end
end
create_node(xml_node, path) click to toggle source

Localiza un nodo y si no existe lo crea

# File lib/CapicuaGen/Tools/xml_helper.rb, line 54
def self.create_node(xml_node, path)

  paths= path.split(/\//).select { |x| !x.blank? }


  current_node_name= paths.shift

  # Alcance el ultimo nodo lo creo
  if paths.count==0
    current_node= Nokogiri::XML::Node.new(clear_node_name(current_node_name), xml_node)
    xml_node.add_child(current_node)
    return current_node
  end

  # Si no existe lo creo
  current_node= xml_node.xpath(current_node_name).first
  unless current_node
    current_node= Nokogiri::XML::Node.new(clear_node_name(current_node_name), xml_node)
    xml_node.add_child(current_node)
  end

  return create_node current_node, paths.join('/')

end
format(xml_string) click to toggle source

Formate el xml

# File lib/CapicuaGen/Tools/xml_helper.rb, line 90
def self.format(xml_string)
  doc= REXML::Document.new(xml_string)
  doc.context[:attribute_quote]= :quote
  formatted= ""
  doc.write(formatted, 2)
  return formatted
end