How to read a xml file?
I have this XML source:
-------------------------------------------------------
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<fichas>
<ficha>
<nombre>Gabriel</nombre>
<apellido>Molina</apellido>
<direccion>Alfredo Vargas #36</direccion>
</ficha>
<ficha>
<nombre>Jorge</nombre>
<apellido>Mendoza</apellido>
<direccion>Alguna de por ahi #12</direccion>
</ficha>
</fichas>
-------------------------------------------------------
And I have this RoR source:
-------------------------------------------------------
require "rexml/document"
include REXML
doc = Document.new File.new("xml_info.xml")
first_name = Array.new
last_name = Array.new
address = Array.new
n = 0
doc.elements.each("a/b/first") { |name|
first_name << name.text.to_s
n += 1
}
doc.elements.each("a/b/last") { |name|
last_name << name.text.to_s
}
doc.elements.each("a/b/dirs") { |dir|
address << dir.text.to_s
}
0.upto(n-1) do |i|
puts first_name[i]
puts last_name[i]
puts address[i]
end
-------------------------------------------------------
And it works, but when I work with this xml file, it doesn't show
anything.
My new XML file:
-------------------------------------------------------
<?xml version="1.0" encoding="utf-8" ?>
<env:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:env="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<env:Body>
<n1:Message xmlns:n1="urn:ActionWebService"
env:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<name xsi:type="xsd:string">Jorge</name>
<last xsi:type="xsd:string">Mendoza</ape>
</n1:Message>
</env:Body>
</env:Envelope>
-------------------------------------------------------
My new RoR file:
-------------------------------------------------------
require "rexml/document"
include REXML
def test
doc = Document.new File.new("xml_info.xml")
first_name = Array.new
last_name = Array.new
doc.elements.each("env:Envelope/env:Body/n1/name") { |name|
first_name << name.text.to_s
}
doc.elements.each("env:Envelope/env:Body/n1/last") { |name|
last_name << name.text.to_s
}
# This function must return the message "Hello Jorge Mendoza"
"Hello " + first_name[0] + " " + last_name[0]
end # test
-------------------------------------------------------
The last xml and ruby source are of a web service.
Can Anybody help me?
--
Posted via http://www.ruby-forum.com/.
--
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.