Skip to content

Commit d436705

Browse files
hapibenbarmintor
authored andcommitted
Strip invalid XML characters.
1 parent d676c18 commit d436705

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

lib/oai/client.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,7 @@ def load_document(xml)
251251
end
252252
when 'rexml'
253253
begin
254+
xml = strip_invalid_xml_chars(xml)
254255
return REXML::Document.new(xml)
255256
rescue REXML::ParseException => e
256257
raise OAI::Exception, 'response not well formed XML: '+e.message, caller
@@ -354,5 +355,18 @@ def strip_invalid_utf_8_chars(xml)
354355
xml
355356
end
356357

358+
def strip_invalid_xml_chars(xml)
359+
invalid = false
360+
361+
begin
362+
REXML::Document.new(xml)
363+
rescue REXML::ParseException => e
364+
invalid = true
365+
end
366+
367+
return xml.gsub!(/&(?!(?:amp|lt|gt|quot|apos);)/, '&') if invalid
368+
return xml
369+
end
370+
357371
end
358372
end

0 commit comments

Comments
 (0)