Reading malformed XML with Nokogiri: Unescaped Ampersands in URL field

廉价感情. 提交于 2019-12-01 08:30:37

问题


I am trying to read a XML file from a third party with Nokogiri in my rails project. One of the nodes I have ot parse contains an URL with unescaped ampersands (like foo.com/index.html?page=1&query=bar)

I understand that this is considered malformed XML, and Nokogiri just tries to parse it anyway, resulting in foo.com/index.html?page=1=bar.

How can I obtain the full URL? Can I tweak Nokogiri? Would you do a search&replace-prerun or what would be the best practice?


回答1:


Had the same issue parsing SVGs with image links containing ampersands.

Parsing SVGs as HTML seems to correctly handle the links, escaping &.

fixed_svg = Nokogiri::HTML.fragment(raw_svg).to_html
# proceed with XML parsing
svg = Nokogiri::XML(fixed_svg)


来源:https://stackoverflow.com/questions/13608666/reading-malformed-xml-with-nokogiri-unescaped-ampersands-in-url-field

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!