tags:

views:

28

answers:

1

ref xml doc

I want to parse on EACH <HOST> and get the number of <OID> , along with Values of each

<TYPE> 
<HOST>
  <IP>10.1.3.1</IP>
  <TRACKING_METHOD>IP</TRACKING_METHOD>
  <DNS><![CDATA[.demo.com]]></DNS>
  <OPERATING_SYSTEM><![CDATA[abcd Version 7]]></OPERATING_SYSTEM>
  <ASSET_GROUPS>
    <ASSET_GROUP_TITLE><![CDATA[Demo Lab ]]></ASSET_GROUP_TITLE>
    <ASSET_GROUP_TITLE><![CDATA[Demo Lab cd]]></ASSET_GROUP_TITLE>
  </ASSET_GROUPS>
  <VULN_INFO_LIST>
    <VULN_INFO>
      <QID id="qid_43172">43172</QID>
      <TYPE>Vuln</TYPE>
      <SSL>false</SSL>
      <FIRST_FOUND>2009-10-27T18:38:29Z</FIRST_FOUND>
      <LAST_FOUND>2010-05-10T18:22:11Z</LAST_FOUND>
      <TIMES_FOUND>4</TIMES_FOUND>
      <VULN_STATUS>Active</VULN_STATUS>
    </VULN_INFO>
    <VULN_INFO>
      <QID id="qid_43142">43142</QID>
      <TYPE>Vuln</TYPE>
      <SSL>false</SSL>
      <FIRST_FOUND>2009-05-08T17:41:01Z</FIRST_FOUND>
      <LAST_FOUND>2010-05-10T18:22:11Z</LAST_FOUND>
      <TIMES_FOUND>5</TIMES_FOUND>
      <VULN_STATUS>Active</VULN_STATUS>
    </VULN_INFO>
  </VULN_INFO_LIST>
</HOST>
<HOST>
A: 

You can use Nokogiri and do something like:

require 'nokogiri'

xml_string = #load the xml however you want here
doc = Nokogiri::XML(xml_string)

doc.css("HOST").each do |host|
  puts "host #{host.at_css('IP').text}"

  qids = host.css("QID")
  puts "#{qids.length} QIDs"
  qids.each do |qid| 
    puts qid.text
  end

  puts "---"
end
JHurrah
I apologize for the Typo , i meant <QID> instead of <OID> You are right , Jakub, there are no <OID> tags.
kamal