Forum: Ruby Need helping parsing XML data retrieved from rest call

352d42c158e96c9fb4c0e7737cabd765?d=identicon&s=25 Charlie B. (charlie_b84)
on 2012-12-18 21:27
I wrote a web service in C#/ASP and published in IIS.  This web service
performs a function for Windows servers during the build process.  I
need the client to make the web call and grab the auto-generated GUID
that is returned from the service and write it to a text file.  However
I am having issues pulling out just what I need.

Here is an example of the data returned from the call, which is done
using rest-client in Ruby:

<?xml version="1.0" encoding="utf-8"?>
<string xmlns="companyname.confidential.automation">THIS IS THE DATA I
NEED TO KEEP FROM THIS WEB CALL</string>

How do I pull out JUST the bolded stuff above?  Note there are no spaces
in the GUID that is generated.

I don't believe xmlsimple will do what I want.

I'd rather avoid using something like str.sub(target, '') on this kind
of call because of obvious reasons.  I am open to any suggestions.
Thanks!
75eb154a2d05884a8a6a8e54b6eb3deb?d=identicon&s=25 Wayne Brisette (Guest)
on 2012-12-18 21:42
(Received via mailing list)
Look at nokogiri. http://nokogiri.org

It will parse the data you are looking for.

Wayne
352d42c158e96c9fb4c0e7737cabd765?d=identicon&s=25 Charlie B. (charlie_b84)
on 2012-12-18 22:32
Actually I was looking past the real issue, which was a problem with the
data being returned.  Needed to throw in a base64 encode and decode to
get the right XML data so the parser didn't flip out.  Thanks Wayne for
jumping in fast.  I will take a look at nokogiri because I think I will
need it for the next step in my project.
54404bcac0f45bf1c8e8b827cd9bb709?d=identicon&s=25 7stud -- (7stud)
on 2012-12-19 00:32
require 'nokogiri'

xml =<<MY_XML
<?xml version="1.0" encoding="utf-8"?>
<string xmlns="companyname.confidential.automation">THIS IS THE DATA I
NEED TO KEEP FROM THIS WEB CALL</string>
MY_XML

doc = Nokogiri::XML(xml)
puts doc.root.text()
puts doc.xpath('//text()[1]')

--output:--
THIS IS THE DATA I NEED TO KEEP FROM THIS WEB CALL
THIS IS THE DATA I NEED TO KEEP FROM THIS WEB CALL
20753db11b9ffec13351e7fc2ddfbf7e?d=identicon&s=25 Ricky Ng (Guest)
on 2012-12-19 01:14
(Received via mailing list)
Other option is REXML.
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.