Thursday, 26 February 2015

Read/Write XML in Ruby using Nokogiri

require 'nokogiri'

$imgNode= "<METADATA order=\"22\" key=\"Key22\"><![CDATA[2 cm]]></METADATA>"class ReadXMLNodes  def initialize()
    @file = File.open('../Orders/ShoeTemplate.xml')
    @xmlObj= Nokogiri::XML(@file)
    puts @xmlObj.to_s
    puts 'Node Count: '+ @xmlObj.xpath('//PRODUCT/P_NR').count.to_s
  end
  def getNode(strNodeXPath)
    @xmlObj.xpath(strNodeXPath)
  end
  def saveXML()
    @timeStamp= Time.new
    timestmp= @timeStamp.strftime("%Y-%m-%d-%H-%M-%S-%s")
    File.delete('../Results/FileOutput'+ timestmp+ ".xml") if File.exist? ('../Results/FileOutput'+ timestmp+ ".xml")
    @fileWr=File.new('../Results/FileOutput'+ timestmp+ ".xml", "w")
    @fileWr.write(@xmlObj.to_xml)
    @fileWr.close
  end
  def getFirstNode(strNodeXPath)
    @xmlObj.xpath(strNodeXPath).first()  #Or    # @xmlObj.xpath(strNodeXPath)[0]  end
  def getLasttNode(strNodeXPath)
    @xmlObj.xpath(strNodeXPath).last()  #Or    # @xmlObj.xpath(strNodeXPath)[0]  end
  def readFirstNodeContent(strXpath, strAttrVal)
    # @xmlObj.xpath("//P_ACTIVE[@channel='"+ strAttrVal + "']")[0].content    @xmlObj.xpath(strXpath + "'"+ strAttrVal + "']")[0].content  #If we dont keep content here, it reads the whole node value  end
  def readLastNodeContent(strXpath, strAttrVal)
    # @xmlObj.xpath("//P_ACTIVE[@channel='"+ strAttrVal + "']")[0].content    @xmlObj.xpath(strXpath + "'"+ strAttrVal + "']").last().content  #If we dont keep content here, it reads the whole node value  end
  def readNodeContent(strXpath, index)
    # @xmlObj.xpath(strXpath).last().content    @xmlObj.xpath(strXpath)[index.to_i].content
  end
  def removeNode(strNodeXPath)
    @xmlObj.xpath(strNodeXPath).remove
  end  def removeFirstNode(strNodeXPath)
    @xmlObj.xpath(strNodeXPath)[0].remove
  end  def removeLastNode(strNodeXPath)
    @xmlObj.xpath(strNodeXPath).last().remove
  end
  def countNodes(strXpath)
    @xmlObj.xpath(strXpath).count
  end
  def modify_node_content(strXpath, strValue)
    @xmlObj.xpath(strXpath)[0].content=strValue  end
  def AddNode(strXpath, strNode, index)
    if @xmlObj.xpath(strXpath).count >= 0      @xmlObj.xpath(strXpath)[index].add_next_sibling(strNode)
    end  end
  def getAttributeValue(strXpath, strAttribute)
    node= @xmlObj.xpath(strXpath)[0]
    node.attributes[strAttribute].value
  end  def modify_attribute_val(strXpath, strAttribute, val_to_assign)
    node= @xmlObj.xpath(strXpath)[0]
    node.attributes[strAttribute].value=val_to_assign  endend

readNodes= ReadXMLNodes.new
puts 'First Node is: '+ readNodes.getFirstNode('//A_MEDIADATA/A_MEDIA')
puts 'Last Node is: '+ readNodes.getLasttNode('//A_MEDIADATA/A_MEDIA')
puts 'Convert to xml: '+ readNodes.getLasttNode('//A_MEDIADATA/A_MEDIA').to_xml

# puts 'Read Node: '+ readNodes.getNode('//P_METADATA/METADATA')# puts 'Remove Node: '+ readNodes.removeNode('//P_METADATA/METADATA')# puts 'Read Node: '+ readNodes.getNode('//P_METADATA/METADATA')
puts '++++++++++++++++++ First Read+++++++++++++++++++++++++++'puts readNodes.getNode('//P_METADATA/METADATA')
puts '++++++++++++++++++ Remove Node +++++++++++++++++++++++++++'puts readNodes.removeLastNode('//P_METADATA/METADATA')
puts readNodes.removeNode('//P_METADATA/METADATA')
puts '++++++++++++++++++ Read After +++++++++++++++++++++++++++'puts readNodes.getNode('//P_METADATA/METADATA')
puts readNodes.readFirstNodeContent('//P_ACTIVE[@channel=', 'tiau')
puts readNodes.readLastNodeContent('//A_PRICEDATA/A_PRICE[@channel=', 'test')
puts readNodes.readLastNodeContent('//A_MEDIADATA/A_MEDIA[@type=', 'image')
puts 'Print Node content'puts readNodes.readNodeContent("//A_MEDIADATA/A_MEDIA[@type='image']", 4)

print "Counting Nodes: "puts readNodes.countNodes("//A_MEDIADATA/A_MEDIA")
puts readNodes.modify_node_content("//P_ACTIVE[@channel='test']", "Venu")
puts readNodes.modify_node_content("//ARTICLEDATA/ARTICLE/NR", "999999")
puts "testing"puts readNodes.AddNode('//A_MEDIADATA/A_MEDIA', $imgNode, 4)
puts 'reading attribute value'puts readNodes.getAttributeValue('//P_ACTIVE', 'channel')
puts 'modify attribute value: 'readNodes.modify_attribute_val('//P_ACTIVE', 'channel', 'AttributeValChanged')
print 'Changed attribute value is: 'puts readNodes.getAttributeValue('//P_ACTIVE', 'channel')
puts readNodes.saveXML

=begin1. Add Nodes2. Add Attribute3. Modify Attribute4. Remove Attribute5. Read Attribute value6.=end

No comments:

Post a Comment