如何使我的xml安全地进行解析(当它具有&字符时)? [英] How can I make my xml safe for parsing (when it has & character in it)?

查看:205
本文介绍了如何使我的xml安全地进行解析(当它具有&字符时)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经获得了一个xml字符串,我需要通过解析器。它目前因为非法的xml字符而抱怨。非常简单的例子:

I've been given an xml string which I need to put through a parser. Its currently complaining because of an illegal xml character. Very simplified example:

<someXml>this & that</someXml>

我知道解决方案是替换& 使用& amp; ,但我没有生成XML,因此无法控制值。

I know that the solution is to replace & with &amp;, but I'm not generating the XML and therefore have no control over the values.

简单的字符串替换不是正确的方法,因为'&'在XML中具有特殊含义,并且'&'与'&'的全局替换; amp;'会破坏预期的特殊含义。是否有一个解决方案来获取一个完整的xml文档并修复它以便'&'成为'& amp;',但只是在预定的位置?我可以安全地全球取代'& 'with'& amp; '(注意两边的空格)?

A simple string replace is not the right way to to this since the '&' has special meaning in XML and a global replace of '&' with '&amp;' would ruin the special meaning which was intended. Is there a solution to take a full xml document and 'fix' it so that '&' become '&amp;', but only where intended? Am I safe to globally replace ' & ' with ' &amp; ' (note the spaces on either side)?

推荐答案

我认为这是一个有趣的问题,因为这种情况可能真的发生了在真实生活中。虽然我认为正确的做法是要求XML提供程序修复XML并使其有效,但我认为有一种选择是尝试使用宽松的解析器。我做了一些搜索,我找到了这个博客帖子谈论同样的问题,并提出我所想到的相同解决方案。您可以尝试使用 jsoup 。让我再说一遍,我认为这不是最好的事情:你应该要求XML提供商修复它。

I think this an interesting question, because it's a situation that may really happen in real-life. Although I believe that the right thing to do is asking the XML provider to fix the XML and make it valid, I thought one option was trying with a lenient parser. I did some search and I found this blog post talking about this same problem, and suggesting the same solution that I was think of. You may try with jsoup. Let me repeat that I think this is not the best thing to do: you should really ask the XML provider to fix it.

这篇关于如何使我的xml安全地进行解析(当它具有&amp;字符时)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆