最近使用apache common的XMLConfiguration来解析XML文件,最后发现这个工具在截取XML时遇到英文逗号,会自动截断。导致XML中属性值等不能用这个符号。
开始的时候,我用其他符号来代替英文逗号,然后获取到值后替换就行了。可是后来觉得这个真是让人烦,就上网查了下,很多说法是禁用使用这个分隔符,方法如下:
XMLConfiguration config = new XMLConfiguration();
config.setDelimiterParsingDisabled(true);
结果发现根本没有这个方法,后来查了源码,由于版本不同我的这个新版本中是用以下的方法:AbstractConfiguration.setDelimiterParsingDisabled(true)的方式来消除。但是我觉得这样的方法似乎也不是很好。最后查看了XMLConfiguration类的源码,其中类上有这样一部分注释:
By inheriting from AbstractConfiguration this class provides some extended functionality, e.g. interpolation of property values. Like in PropertiesConfiguration property values can contain delimiter characters (the comma ',' per default) and are then split into multiple values. This works for XML attributes and text content of elements as well. The delimiter can be escaped by a backslash. As an example consider the following XML fragment:
<config>
<array>10,20,30,40</array>
<scalar>3\,1415</scalar>
<cite text="To be or not to be\, this is the question!"/>
</config>
NND,原理只需要在逗号前家个“\”这样的符号来转义就行了,这样就不用那样设置了。可是之前从网上也没有人这样说,看来有的时候看源码还是比较好,已经屡试不爽了。这里记录下,希望能帮一些同志解决下燃眉之急。