Parse XML POST data with Rails

867 views Asked by At

I'm using REXML to parse through some XML data sent via POST. The XML is sent correctly formatted with "&" sent as "&amp;". However, when I try to parse it, I had problems like the &amp; becoming & and throwing an error. I fixed with gsub, but now it says #<REXML::ParseException: Missing end tag for 'br'

Anyone have any experience parsing through XML sent via POST on Rails?

def parse
xml = params["xml_data"]

xml = xml.gsub(/&/,"&amp;")

doc, posts = REXML::Document.new(xml), []
end
1

There are 1 answers

1
WarHog On BEST ANSWER

The problem most likely is that you send html istead of xml and html isn't valid xml - don't try to use regexp or xml processors for its parsing. In your case you likely encountered <br> tag (not <br /> pay attention) - that perfectly ok for html. Try to use Nokogiri gem for html parsing