Tuesday, 24 March 2015

xml builder in ruby- remove namespace etc

# updates node content 
def update_node_content(file_obj, key, index, code)
  getxml_elements_object(file_obj,
                         getxpath_forkey(key))[index].content = code.call
end# updates node content 
def update_node_native_content(file_obj, key, index, code)
  getxml_elements_object(file_obj,
                         getxpath_forkey(key))[index].native_content = code.call
end
def get_node(xmlFileObject, strNodeXPath)
  xmlFileObject.xpath(strNodeXPath)
end
def get_node_at(xmlFileObject, strNodeXPath, index=0)
  xmlFileObject.xpath(strNodeXPath)[index]
end
def add_node(xmlFileObject, strXpath, strNode, index)
  if xmlFileObject.xpath(strXpath).count >= 0    xmlFileObject.xpath(strXpath)[index].add_next_sibling(strNode)
    xmlFileObject  endend
# gets node countdef get_node_count(file_obj, key)
  getxml_elements_object(file_obj, getxpath_forkey(key)).count.to_s
end
# gets countdef get_count(xmlFileObject, xpath)
  xmlFileObject.xpath(xpath).count.to_s
end
# gets node contentdef get_node_content(file_obj, key, index)
  getxml_elements_object(file_obj, getxpath_forkey(key))[index].content
end
def remove_nodes_but_one(file_obj, xpath_key)
  count = file_obj.xpath(getxpath_forkey(xpath_key)).length
  while count > 1    file_obj.xpath(getxpath_forkey(xpath_key))[count-1].remove
    count-=1  endend

# xmlObject = setattribute_value(node,'key',"AUTI")
 def setattribute_value(node, attributename, valueto_assign)
  node.attributes[attributename].value = valueto_assign 
end


remove namespace from xml:
# xmlObject = get_xml_object("../Files/Product.xml") 
def get_xml_object(path)
  @file = File.read(path)
  xmlObj = Nokogiri::XML(@file, nil, 'utf-8')
  @get_namespaces = xml_obj.namespaces
  xmlObj.remove_namespaces!
  xmlObj 
end

in xml file:
<?xml version="1.0" encoding="utf-8"?>
 <order xmlns="http://testing.pic/party/export">

xmlns is the namespace..


No comments:

Post a Comment