From: Zach Dennis Date: 2005-08-29T14:15:01+09:00 Subject: Re: Help needed with rexml What type of information do you want to get out of this? You never posted what you *thought* your sample ruby code would give you. I ran your perl example, and it looks like the xml document but where < > are gsub'd for { }. Here is an example which shows some xpath usage.: require 'rexml/document' file = File.new("test.xml") root = REXML::Document.new(file).root fault_arr = root.elements.each( "fault" ) do |e1| e1.elements.each( "value/struct/member" ) do |e2| e2.elements.each( '*' ) { |e3| print e3.text.strip } e2.elements.each( '*/*' ){ |e3| puts " " + e3.text.strip } end end puts "Message response faulted!" if fault_arr.length > 0 Zach Michael wrote: > I've been struggling to properly parse some XML with rexml. I will fully > admit my XML ignorance in advance. It would be easy enough to parse > this with a regular expression instead, but I would prefer to use the > right tool. > > Here's a sample XML response: > > > > > > > > faultCode > > 5 > > > > faultString > > system error (nologin) > > > > > > > > However, I can't anything useful out of it. For instance, I've been > trying something like this: > > require 'rexml/document' > > file = File.new("test.xml") > xml = REXML::Document.new(file) > xml.elements.each { |i| > i.texts.each { |t| > puts "Class: #{t.class}" > puts "Value: #{t.value}" > puts "String: #{t.to_s}" > } > } > > This doesn't print anything useful for the class. Where am I going wrong > with this? I've been digging through the documentation but I'm must not > getting it. > > For what it's worth, I can parse this in perl easily enough (which > suggests to me the XML is valid): > > use Data::Dumper; > use XML::Simple; # AKA "XML For Idiots" > > my $ref = XMLin("test.xml"); # A file containing the XML above > print Dumper $ref, "\n"; > > I can then use the results to figure out how to dereference $ref to pull > the error information returned by the server. > > Responses to the list or the newsgroup, please, for future googlers > to find.