# Test the value of an XML node

**URL:** <https://www.ruby-forum.com/t/test-the-value-of-an-xml-node/145824>\
**Category:** Rails\
**Created:** [August 14, 2008, 9:41am UTC](https://www.ruby-forum.com/t/test-the-value-of-an-xml-node/145824 "2008-08-14T09:41:20Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![iga](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/e19b73/32.png) [@iga](https://www.ruby-forum.com/u/iga)\
**Post date:** [August 14, 2008, 9:41am UTC](https://www.ruby-forum.com/t/test-the-value-of-an-xml-node/145824/1 "2008-08-14T09:41:20Z")

</div>

I’m accessing a log in scheme from a web service and I want to be able  
to check the returned values in order provide some form of validation  
and error checking.

For example, when I submit the log in request, the service returns an  
XML String something like the following…

\<?xml version="1.0" encoding="utf-16"?\> `InvalidPassword` false Invalid password

I’d like to be able to display the value [text] of Message as well as  
re-direct users if Success is “false” for example…

def login  
#get the value of Code and test  
if @code = ‘False’  
flash[:notice] = “Something failed…”  
redirect\_to :action =\> “index”  
else  
@message = ‘Welecome’  
end  
end

---

<div class="post-metadata">

**Author:** ![iga](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/e19b73/32.png) [@iga](https://www.ruby-forum.com/u/iga)\
**Post date:** [August 14, 2008, 10:13am UTC](https://www.ruby-forum.com/t/test-the-value-of-an-xml-node/145824/2 "2008-08-14T10:13:33Z")

</div>

On Aug 14, 2:05 am, iga [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I’m accessing a log in scheme from a web service and I want to be able  
> to check the returned values in order provide some form of validation  
> and error checking.
> 
> For example, when I submit the log in request, the service returns an  
> XML String something like the following…

Bung it through a xml parser. REXML comes with ruby (there are faster  
things based on native libraries but if you’re only looking at small  
documents like that it won’t make any difference). Off the top of my  
head the code would look something like  
require ‘rexml/document’  
…

document = REXML::Document.new(my\_xml\_string)  
root = document.root  
message = root.elements[1, ‘Message’].text

Fred
