# How to read a xml file?

**URL:** <https://www.ruby-forum.com/t/how-to-read-a-xml-file/185832>\
**Category:** Rails\
**Created:** [March 26, 2010, 8:48pm UTC](https://www.ruby-forum.com/t/how-to-read-a-xml-file/185832 "2010-03-26T20:48:12Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jorgegetafe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/eada6e/32.png) [@jorgegetafe](https://www.ruby-forum.com/u/jorgegetafe)\
**Post date:** [March 26, 2010, 8:48pm UTC](https://www.ruby-forum.com/t/how-to-read-a-xml-file/185832/1 "2010-03-26T20:48:12Z")

</div>

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](http://www.w3.org/2001/XMLSchema)” xmlns:env=“[http://schemas.xmlsoap.org/soap/envelope/](http://schemas.xmlsoap.org/soap/envelope/)” xmlns:xsi=“[http://www.w3.org/2001/XMLSchema-instance](http://www.w3.org/2001/XMLSchema-instance)”\> env:Body \<n1:Message xmlns:n1=“urn:ActionWebService” env:encodingStyle=“[http://schemas.xmlsoap.org/soap/encoding/](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?

---

<div class="post-metadata">

**Author:** ![jorgegetafe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/eada6e/32.png) [@jorgegetafe](https://www.ruby-forum.com/u/jorgegetafe)\
**Post date:** [March 26, 2010, 8:52pm UTC](https://www.ruby-forum.com/t/how-to-read-a-xml-file/185832/2 "2010-03-26T20:52:52Z")

</div>

There are mistakes in the first xml source, here is the good source:

> ## \<?xml version="1.0" encoding="UTF-8" standalone="yes"?\> Gabriel Molina Alfredo Vargas #36 Jorge Mendoza Alguna de por ahi #12

Can Anybody help me?

---

<div class="post-metadata">

**Author:** ![jorgegetafe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/eada6e/32.png) [@jorgegetafe](https://www.ruby-forum.com/u/jorgegetafe)\
**Post date:** [March 27, 2010, 4:01pm UTC](https://www.ruby-forum.com/t/how-to-read-a-xml-file/185832/3 "2010-03-27T16:01:23Z")

</div>

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-
