How to read a xml file?
I have this XML source:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> Gabriel Molina Alfredo Vargas #36 Jorge Mendoza Alguna de por ahi #12 -------------------------------------------------------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/”>
Jorge
Mendoza
</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?