Ticket #147: xml_test.rb

File xml_test.rb, 0.8 kB (added by patrick, 4 months ago)

run init once and resave several time

Line 
1require "rexml/document"
2
3def init
4  file = File.new( 'test.xml' )
5  doc = REXML::Document.new file
6  file.close
7 
8  menu = doc.get_elements('/menu')[0]
9 
10  list = ["&&blah", "mehr_bla&h"]
11  list2 = ["ajfh&&jfsd", "jshd"]
12 
13  res = REXML::Element.new 'entry'
14  res.attributes['text'] = list.inspect
15  res.attributes['text_untouched'] = list2.inspect
16 
17  menu.elements << res
18 
19  file = File.new( 'test2.xml', 'w' )
20  doc.write( file, 1, false, true )
21  file.close 
22end
23
24def resave
25
26  file = File.new( 'test2.xml' )
27  doc = REXML::Document.new file
28  file.close
29 
30  nodes = doc.get_elements('/menu/entry')
31  list = eval( nodes[0].attributes['text'] )
32 
33  puts list
34 
35  nodes[0].attributes['text'] = list.inspect
36 
37  file = File.new( 'test2.xml', 'w' )
38  doc.write( file, 1, false, true )
39  file.close
40
41end