Second part contains simple code for creating a document
authorEfthimios Toulas <etoulas@suse.de>
Thu, 24 Nov 2005 18:13:59 +0000 (18:13 +0000)
committerEfthimios Toulas <etoulas@suse.de>
Thu, 24 Nov 2005 18:13:59 +0000 (18:13 +0000)
devel/devel.etoulas/helix2yum.rb

index 0173263..ec531ac 100755 (executable)
@@ -4,6 +4,7 @@ require 'rexml/document'
 include REXML
 
 FILENAME = 'test.xml'
+OUTFILE = 'foobar.xml'
 
 if !File.exists?( FILENAME )
   puts 'File not found.'
@@ -11,13 +12,16 @@ if !File.exists?( FILENAME )
 elsif !File.readable?( FILENAME )
   puts 'File not readable.'
   exit 1
+elsif File.zero?( FILENAME )
+  puts 'Nothing to do'
+  exit 0
 end
 
 # puts "Opening file #{FILENAME} (#{FILENAME.size} bytes)"
 printf( "Opening file %s (%d bytes)\n", FILENAME, FILENAME.size )
 file = File.open( FILENAME )
 
-# use file to instanciate an XML-Object
+### use file to instanciate an XML-Object
 doc = Document.new file
 
 ### Finds and returns the first node that matches the supplied xpath
@@ -35,4 +39,21 @@ printf( "elements matching: %d\n", a.length )
 # get Attributes
 # doc.elements.each( 'channel/subchannel/package' ) { |element| puts element.attributes['name'] }
 
+#===============================================================================
+
+file2 = File.new( OUTFILE, 'w+' )
+doc2 = Document.new
+
+elem = Element.new "foo"
+
+elem2 = elem.add_element "bar", { "attrib"=>"2" }
+elem2.text = "this is my text"
+
+elem2 = elem.add_element "hola"
+elem2.text = "amigo"
+
+doc2.add_element elem
+doc2.write( file2, 0 )
+
 file.close
+file2.close