Zur Weihnachtszeit sollte man es ja etwas gemütlicher angehen lassen. Also eine kleine REXML-Geschichte. Das Skript importiert ein Nested Set in die Datenbank von Rails.
#
#
#
DEBUG=0
require "rexml/document"
include REXML
class XmlCategoryImporter
def initialize
end
def insert_node(xmlDoc, xmlCategory, nodeParent, nodeRoot, nLevel)
sStatus = 'UNKNOWN'
sName = xmlCategory.elements['name'].text.to_s
$stdout.puts "sName: #{sName}" if DEBUG==1
if nodeParent.nil?
$stdout.puts "nodeParent == nil" if DEBUG==1
$stdout.puts "Look for 'name = #{sName} AND parent_id IS NULL'" if DEBUG==1
nodeCategory = Category.find(
:first,
:conditions => [ "name = ? AND parent_id IS NULL", sName ]
)
if nodeCategory.nil?
$stdout.puts "nodeCategory == #{nodeCategory}" if DEBUG==1
nodeCategory = Category.new( 'name' => sName )
nodeCategory.save
nodeCategory.update_attribute( :root_id, nodeCategory.id)
sStatus = 'INSERTED'
$stdout.puts "new nodeCategory created" if DEBUG==1
else
$stdout.puts "nodeCategory found in database" if DEBUG==1
sStatus = 'SKIPPED (ALREADY EXISTS)'
end
nodeRoot = nodeCategory
else
$stdout.puts "nodeParent given" if DEBUG==1
nParentId = nodeParent.id
nRootId = nodeRoot.id
$stdout.puts "Look for 'name = #{sName} AND parent_id = #{nParentId} AND root_id = #{nRootId}'" if DEBUG==1
nodeCategory = Category.find(
:first,
:conditions => [ "name = ? AND parent_id = ? AND root_id = ?", sName, nParentId, nRootId ]
)
if nodeCategory.nil?
$stdout.puts "nodeCategory == #{nodeCategory}" if DEBUG==1
nodeCategory = Category.new(
:name => sName,
:parent_id => nodeParent.id,
:root_id => nodeRoot.id
)
nodeCategory.save
sStatus = 'INSERTED'
$stdout.puts "new nodeCategory created" if DEBUG==1
else
sStatus = 'SKIPPED (ALREADY EXISTS)'
$stdout.puts "nodeCategory found in database" if DEBUG==1
end
nodeCategory.move_to_child_of(nodeParent)
end
# sParent = xmlCategory.elements['parent'].text.to_s
# sRoot = xmlCategory.elements['root'].text.to_s
# puts " "*nLevel + "#{sName} (parent: '#{sParent}', root: '#{sRoot}')"
puts " "*nLevel + "#{sName} Status: #{sStatus}"
nodeParent = nodeCategory
puts "-"*50 if DEBUG==1
xmlDoc.elements.each("/categories/category[parent='#{sName}']") do |xmlCategory|
insert_node(xmlDoc, xmlCategory, nodeParent, nodeRoot, nLevel+1)
end
end
#
# Importiert Kategorien aus einer Xml-Datei
# und fügt sie in die DB ein
#
def import(sFileName)
#Category.destroy_all
file = File.new( sFileName )
xmlDoc = REXML::Document.new( file )
xmlDoc.elements.each("/categories/category[name=root]") do |xmlCategory|
insert_node(xmlDoc, xmlCategory, nil, nil, 0)
end
file.close
end
end
Dazu ein passender rake-Task
namespace
ml do
desc "Import categories"
task :import_groups => :environment do
require "#{RAILS_ROOT}/lib/xml_category_importer.rb"
importer = XmlCategoryImporter.new
importer.import( "#{RAILS_ROOT}/staff/new_node.xml" )
end
end
und eine passende XML-Datei.
<?xml version="1.0" encoding="utf-8"?>
<categories>
<category>
<name>Haus_Garten_Tiere</name>
<parent />
<root>Haus_Garten_Tiere</root>
</category>
<category>
<name>Haus</name>
<parent>Haus_Garten_Tiere</parent>
<root>Haus_Garten_Tiere</root>
</category>
<category>
<name>Garten</name>
<parent>Haus_Garten_Tiere</parent>
<root>Haus_Garten_Tiere</root>
</category>
<category>
<name>Tiere</name>
<parent>Haus_Garten_Tiere</parent>
<root>Haus_Garten_Tiere</root>
</category>
<category>
<name>Hunde</name>
<parent>Tiere</parent>
<root>Haus_Garten_Tiere</root>
</category>
<category>
<name>Katze</name>
<parent>Tiere</parent>
<root>Haus_Garten_Tiere</root>
</category>
<category>
<name>Computer</name>
<parent />
<root>Computer</root>
</category>
<category>
<name>Hardware</name>
<parent>Computer</parent>
<root>Computer</root>
</category>
<category>
<name>Software</name>
<parent>Computer</parent>
<root>Computer</root>
</category>
<category>
<name>Grafik</name>
<parent>Software</parent>
<root>Computer</root>
</category>
<category>
<name>Photoshop</name>
<parent>Grafik</parent>
<root>Computer</root>
</category>
<category>
<name>Internet</name>
<parent>Computer</parent>
<root>Computer</root>
</category>
<category>
<name>Programmierung</name>
<parent>Computer</parent>
<root>Computer</root>
</category>
<category>
<name>Ruby</name>
<parent>Programmierung</parent>
<root>Computer</root>
</category>
<category>
<name>Ruby on Rails</name>
<parent>Ruby</parent>
<root>Computer</root>
</category>
<category>
<name>Datenbanken</name>
<parent>Computer</parent>
<root>Computer</root>
</category>
<category>
<name>MySQL</name>
<parent>Datenbanken</parent>
<root>Computer</root>
</category>
<category>
<name>Spiele</name>
<parent />
<root>Spiele</root>
</category>
<category>
<name>PC-Spiele</name>
<parent>Spiele</parent>
<root>Spiele</root>
</category>
<category>
<name>Adventure</name>
<parent>PC-Spiele</parent>
<root>Spiele</root>
</category>
<category>
<name>Konsolen</name>
<parent>Spiele</parent>
<root>Spiele</root>
</category>
<category>
<name>Playstation</name>
<parent>Konsolen</parent>
<root>Spiele</root>
</category>
<category>
<name>Playstation 2</name>
<parent>Konsolen</parent>
<root>Spiele</root>
</category>
<category>
<name>Playstation 3</name>
<parent>Konsolen</parent>
<root>Spiele</root>
</category>
<category>
<name>X-Box</name>
<parent>Konsolen</parent>
<root>Spiele</root>
</category>
<category>
<name>Xbox 360</name>
<parent>Konsolen</parent>
<root>Spiele</root>
</category>
<category>
<name>Wii</name>
<parent>Konsolen</parent>
<root>Spiele</root>
</category>
<category>
<name>Konsolen Allgemein</name>
<parent>Konsolen</parent>
<root>Spiele</root>
</category>
<category>
<name>Auto_Verkehr</name>
<parent />
<root>Auto_Verkehr</root>
</category>
<category>
<name>Verkehrsrecht</name>
<parent>Auto_Verkehr</parent>
<root>Auto_Verkehr</root>
</category>
<category>
<name>Auto</name>
<parent>Auto_Verkehr</parent>
<root>Auto_Verkehr</root>
</category>
<category>
<name>Hersteller</name>
<parent>Auto</parent>
<root>Auto_Verkehr</root>
</category>
<category>
<name>Tuning</name>
<parent>Auto</parent>
<root>Auto_Verkehr</root>
</category>
<category>
<name>Technik</name>
<parent>Auto</parent>
<root>Auto_Verkehr</root>
</category>
<category>
<name>CarHifi</name>
<parent>Auto</parent>
<root>Auto_Verkehr</root>
</category>
<category>
<name>Motorrad</name>
<parent>Auto_Verkehr</parent>
<root>Auto_Verkehr</root>
</category>
<category>
<name>Motorsport</name>
<parent>Auto_Verkehr</parent>
<root>Auto_Verkehr</root>
</category>
<category>
<name>Freizeit</name>
<parent />
<root>Freizeit</root>
</category>
<category>
<name>Fotografie</name>
<parent>Freizeit</parent>
<root>Freizeit</root>
</category>
<category>
<name>Fotografie Menschen</name>
<parent>Fotografie</parent>
<root>Freizeit</root>
</category>
<category>
<name>Natur</name>
<parent>Fotografie</parent>
<root>Freizeit</root>
</category>
<category>
<name>Tiere</name>
<parent>Fotografie</parent>
<root>Freizeit</root>
</category>
<category>
<name>Hunde</name>
<parent>Tiere</parent>
<root>Freizeit</root>
</category>
<category>
<name>Labrador</name>
<parent>Hunde</parent>
<root>Freizeit</root>
</category>
<category>
<name>Katzen</name>
<parent>Tiere</parent>
<root>Freizeit</root>
</category>
<category>
<name>Akt</name>
<parent>Fotografie</parent>
<root>Freizeit</root>
</category>
<category>
<name>Technik</name>
<parent>Fotografie</parent>
<root>Freizeit</root>
</category>
<category>
<name>Architektur</name>
<parent>Fotografie</parent>
<root>Freizeit</root>
</category>
<category>
<name>Sport</name>
<parent>Fotografie</parent>
<root>Freizeit</root>
</category>
<category>
<name>Digiart</name>
<parent>Fotografie</parent>
<root>Freizeit</root>
</category>
<category>
<name>Reise</name>
<parent>Fotografie</parent>
<root>Freizeit</root>
</category>
<category>
<name>Sonstige</name>
<parent>Fotografie</parent>
<root>Freizeit</root>
</category>
<category>
<name>Videos</name>
<parent>Freizeit</parent>
<root>Freizeit</root>
</category>
<category>
<name>Musik</name>
<parent>Freizeit</parent>
<root>Freizeit</root>
</category>
<category>
<name>Sport</name>
<parent>Freizeit</parent>
<root>Freizeit</root>
</category>
<category>
<name>Wissenschaft_und_Technik</name>
<parent />
<root>Wissenschaft_und_Technik</root>
</category>
<category>
<name>Reisen</name>
<parent />
<root>Reisen</root>
</category>
<category>
<name>Deutschland</name>
<parent>Reisen</parent>
<root>Reisen</root>
</category>
<category>
<name>Regionen</name>
<parent>Deutschland</parent>
<root>Reisen</root>
</category>
<category>
<name>Städte</name>
<parent>Deutschland</parent>
<root>Reisen</root>
</category>
<category>
<name>Berlin</name>
<parent>Städte</parent>
<root>Reisen</root>
</category>
<category>
<name>Hamburg</name>
<parent>Städte</parent>
<root>Reisen</root>
</category>
<category>
<name>München</name>
<parent>Städte</parent>
<root>Reisen</root>
</category>
<category>
<name>Europa</name>
<parent>Reisen</parent>
<root>Reisen</root>
</category>
<category>
<name>Asien</name>
<parent>Reisen</parent>
<root>Reisen</root>
</category>
<category>
<name>Vietnam</name>
<parent>Asien</parent>
<root>Reisen</root>
</category>
<category>
<name>Wirtschaft</name>
<parent />
<root>Wirtschaft</root>
</category>
<category>
<name>Wissenschaft_und_Technik</name>
<parent />
<root>Wissenschaft_und_Technik</root>
</category>
<category>
<name>Schulen_Ausbildung_und_Universitäten</name>
<parent />
<root>Schulen_Ausbildung_und_Universitäten</root>
</category>
<category>
<name>Gesundheit</name>
<parent />
<root>Gesundheit</root>
</category>
</categories>
No related posts.


0 Antworten bis jetzt ↓
Es gibt keine Kommentare bis jetzt...Trete Sachen weg, die vom Formular runterfallen.
Hinterlasse ein Kommentar