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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
There are mistakes in the first xml source, here is the good source:> <?xml version="1.0" encoding="UTF-8" standalone="yes"?> > <fichas> > <ficha> > <first>Gabriel</first> > <last>Molina</last> > <dirs>Alfredo Vargas #36</dirs> > </ficha> > <ficha> > <first>Jorge</first> > <last>Mendoza</last> > <dirs>Alguna de por ahi #12</dirs> > </ficha> > </fichas> > -------------------------------------------------------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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Never parsed XML in Ruby before but running a Google search on ''parsing xml in ruby'' brought up tons of links. In any case the second document is malformed. The closing tag for ''last'' is ''ape'', which might give you problems. On Mar 26, 3:52 pm, Jorge alejandro Mendoza torres <li...@ruby- forum.com> wrote:> There are mistakes in the first xml source, here is the good source: > > > <?xml version="1.0" encoding="UTF-8" standalone="yes"?> > > <fichas> > > <ficha> > > <first>Gabriel</first> > > <last>Molina</last> > > <dirs>Alfredo Vargas #36</dirs> > > </ficha> > > <ficha> > > <first>Jorge</first> > > <last>Mendoza</last> > > <dirs>Alguna de por ahi #12</dirs> > > </ficha> > > </fichas> > > ------------------------------------------------------- > > Can Anybody help me? > > -- > Posted viahttp://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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.