From: Kathy Simmons Date: 2006-11-17T02:35:30+09:00 Subject: Re: parse xml file, put results in mysql db Here's the full code - I'm reading in nmap output in scanfile.xml and want to put the data in a mysql db: #! /usr/bin/ruby require 'rexml/document' require "mysql" require "dbi" include REXML scanfile = File.new('scanfile.xml') doc = Document.new(scanfile) root = doc.root doc.elements.each("nmaprun") { |element| puts element.attributes["args"] args = element.attributes["args"] puts element.attributes["startstr"] timeofscan = element.attributes["startstr"] puts element.attributes["version"] version = element.attributes["version"] } doc.elements.each("nmaprun/scaninfo") { |element| puts element.attributes["type"] scantype = element.attributes["type"] puts element.attributes["protocol"] protocol = element.attributes["protocol"] puts element.attributes["numservices"] numservices = element.attributes["numservices"] # puts element.attributes["services"] services = element.attributes["services"] } doc.elements.each("nmaprun/scaninfo/host") { |element| puts element.attributes["status state"] } # database insert dbname="nmap" m = Mysql.new("localhost", "root", "") dbh=DBI.connect("dbi:Mysql:nmap:localhost", "root", "") m.select_db(dbname) sth=dbh.prepare("INSERT INTO rawdata (file,tool,arguments,startime,version) VALUES (?,?,?,?,?)") sth.execute("scanfile.xml", "nmap", "${args}", "#{timeofscan}", "${version}"") -- Posted via http://www.ruby-forum.com/.