#!/usr/bin/ruby require 'xml/simple' # create a string xmldoc = "\n" xmldoc << " Jürgen Christian Emanuel 41\n" xmldoc << "" # parse it doc = XML::Simple.string(xmldoc) # output all guys with a nickname (the ü is a UTF-8 character in the output) # cause (always) everything that is returned is UTF-8 doc.root.find("/contributors/guy[@nick]").each { |ele| puts "#{ele} (#{ele.attributes.include?("ick")})" puts "#{ele} (#{ele.attributes["nick"]})" } # query arthur dent puts doc.root.find("/contributors/guy[@id='4']").first.to_i + 1 # write to a file File.open(File.dirname($0) + "/EXAMPLE.str.xml",File::WRONLY|File::CREAT|File::TRUNC) { |f| doc.save_as f }