edit xml file

D

Dmodie

I'm new to Ruby, and this may be considered advanced. I'll do my best
to describe it. I inherited some code that uses erb to display a web
page form, like this:

get "/" do
$script = File.read("public/script2.xml")
@xml = Nokogiri::XML($script)

erb :index
end

index.erb (attached) seems to get that xml file passed into it. It has
embedded
Ruby code that seems to loop through the xml and assign a bunch of
variables and such.

The user inputs something on the form, and I am supposed to then edit
the xml with that new data. For example a node in the xml has a
attribute of value = "sound1.mp3". On the form the user enters
"sound2.mp3", and now the xml has to get modified to say, value =
"sound2.mp3".

I actually got it all working, except that I am modifying the xml
explicitly (in a post block), using REXML like this:

newSound = params[:newsound]

# open the xml
File.open("public/script2.xml", "r") do |aFile|
config = REXML::Document.new(aFile)
rootVar = config.root.elements['object']

#drill down to the sound node, assign the new value to it
rootVar.elements['property[2]'].elements['array'].elements['object'].elements['property[2]'].elements['array'].elements['object'].elements['property[2]'].attributes["value"]
= newSound

# reopen the file for writing, which will erase it, and copy all the
data into it
formatter = REXML::Formatters::Default.new
File.open('public/script2.xml', 'w') do |result|
formatter.write(config, result)
end
end
# re-read in the xml file, and then load index again
$script_post = File.read("public/script2.xml")
@xml = Nokogiri::XML($script_post)
erb:index


The part I need to fix, for my boss is: the part where I'm drilling down
to assign the new value should be somehow connected to index.erb, which
is doing all kinds of looping and variable assignments relating to the
xml. I don't know what it is called or where to start. Hell I don't
even know how to explain it. Sorry for the long post, any help, or
keywords to look up or examples greatly appreciated.

Attachments:
http://www.ruby-forum.com/attachment/5763/index.erb
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,059
Latest member
cryptoseoagencies

Latest Threads

Top