require'rexml/document'includeREXMLfile =File.read "file.xml"xmldoc =Document.new(xmlfile)# Get the root elementroot = xmldoc.rootputs"Root element : "+ root.attributes["shelf"]# List of movie titles.xmldoc.elements.each("collection/movie") do|e|puts"Movie Title : "+ e.attributes["title"] end# List of movie types.xmldoc.elements.each("collection/movie/type") do|e|puts"Movie Type : "+ e.text end# List of movie description.xmldoc.elements.each("collection/movie/description") do|e|puts"Movie Description : "+ e.textend# List of movie starsxmldoc.elements.each("collection/movie/stars") do|e|puts"Movie Stars : "+ e.textend
Nokogiri
require'nokogiri'
Slop
require'nokogiri'# Parse XML filedoc =Nokogiri::Slop fileputs doc.search("type").map {|f| t.text} # List of Typesputs doc.search("format").map {|f| f.text} # List of Formatsputs doc.search("year").map {|y| y.text} # List of Yearputs doc.search("rating").map {|r| r.text} # List of Ratingputs doc.search("stars").map {|s| s.text} # List of Starsdoc.search("description").map {|d| d.text} # List of Descriptions
JSON
Assume you have a small vulnerability database in a json file like follows
{"Vulnerability": [ {"name":"SQLi","details:": {"full_name":"SQL injection","description":"An injection attack wherein an attacker can execute malicious SQL statements","references": ["https://www.owasp.org/index.php/SQL_Injection","https://cwe.mitre.org/data/definitions/89.html" ],"type":"web" } } ]}
If you want to add to this database, just create a hash with the same struction.
xss = {"name"=>"XSS", "details:"=>{"full_name"=>"Corss Site Scripting", "description"=>" is a type of computer security vulnerability typically found in web applications", "references"=>["https://www.owasp.org/index.php/Cross-site_Scripting_(XSS)", "https://cwe.mitre.org/data/definitions/79.html"], "type"=>"web"}}
You can convert it to json just by using `.to_json` method