Skip to content

Instantly share code, notes, and snippets.

@afair
Created November 23, 2010 03:14
Show Gist options
  • Save afair/711172 to your computer and use it in GitHub Desktop.
Save afair/711172 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'rubygems'
require 'nokogiri'
class Cleaner
def read
f = File.open("nelson.xml")
@doc = Nokogiri::XML(f)
f.close
end
def save
f = File.open("nelson-new.xml", 'w')
f.puts @doc
f.close
end
def find
list = []
# Find each dictionary item and loop through it
@doc.xpath('/plist/dict/dict/dict').each do |node|
hash = {}
last_key = nil
# Stuff the key value pairs in to hash. We know a key is followed by
# a value, so we'll just skip blank nodes, save the key, then when we
# find the value, add it to the hash
node.children.each do |child|
next if child.blank? # Don't care about blank nodes
if child.name == 'key'
# Save off the key
last_key = child.text
else
# Use the key we saved
hash[last_key] = child.text
end
end
if hash['Location'] =~ /\/mp3\/mp3\//
puts hash['Location']
node.remove
end
#list << hash # push on to our list
end
end
end
c = Cleaner.new
c.read
c.find
c.save
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment