From: Eric Hodel Date: 2009-08-25T10:18:11+09:00 Subject: Re: REXML: parsing a string with unescaped ampersand entitie On Aug 24, 2009, at 15:57, Engine Yard wrote: > Sorry, may be I was unclear. I understood. > What happens is: the xml that is sent back contains something like > "cheese & coffee" Right. This is not XML because & is not escaped. > so this breaks REXML, and since the call to REXML's method is made > from the ActiveResource object itself we are unable to customize how > REXML's initialize method is called. > > When I modified the line "cheese & coffee" to "cheese & coffee" > everything works fine Yep, this is escaped and valid for XML. You should file a bug with the website you're consuming telling them they have broken XML output. That doesn't help you solve your problem though! :) > So I am pretty sure this issue is being caused because of the > unescaped ampersand that is contained in the xml. But I cannot > modify the xml content as it comes to me from a third party source > over which we have no control. > > Here is the stack trace that might help clearing up things: > > > --- !ruby/exception:REXML::ParseException > message: |- > # coffee"> > [...] > Yes. ActiveResource and REXML are behaving correctly. They're not really sure what to do with text you've given them as it's not XML. Since you have to deal with reality, you'll want a forgiving XML parser that can handle some invalid XML when you really need to. > How can we change what parser is being used by Rails. I would > definitely > like to try out Nokogiri but am unsure about how to make it work with > Rails (2.3.2) and in specific with ActiveResource though. I'm not sure either. You could either use a different tool than ActiveResource or try contacting the ActiveResource maintainers for help in adding the option of being forgiving of invalid XML. (Nokogiri is good at correcting invalid XML, so I suggested it.)