Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

xml转jsonobject,xml标签中的字符串&被转成了& #3560

Closed
winnie-33 opened this issue Apr 24, 2024 · 1 comment
Closed

xml转jsonobject,xml标签中的字符串&被转成了& #3560

winnie-33 opened this issue Apr 24, 2024 · 1 comment
Labels

Comments

@winnie-33
Copy link

winnie-33 commented Apr 24, 2024

版本情况

JDK版本: openjdk_8_401
hutool版本: 5.8.27

问题描述

xml转jsonobject,xml标签中的字符串&被转成了&,可以做配置不要转吗?
image

代码这样写的

String inPara= "<ROOT><ID>002317479934367853</ID><CONTENT>asdfadf&amp;21sdgzxv&amp;aasfasf</CONTENT></ROOT>";
        cn.hutool.json.JSONObject jsonObject2 = XML.toJSONObject(inPara, true);

第二个参数传true的原因是希望数字不要被转成数字型,需要全部都是字符串型

@looly
Copy link
Member

looly commented Apr 25, 2024

这个转换是没问题的。

我简化你的XML:

<CONTENT>&amp;</CONTENT>

因为&是保留字符,因此在XML中需要转义,变成&amp;,这样在解析时会被反解析为&

你如果想显示原本的内容&amp;,那你的内容必须为:

<CONTENT>&amp;amp;</CONTENT>

或者使用CDATA方式表示原始字符串:

<![CDATA[&amp;]]>

在你的例子中为:

String inPara= "<ROOT><ID>002317479934367853</ID><CONTENT><![CDATA[asdfadf&amp;21sdgzxv&amp;aasfasf]]></CONTENT></ROOT>";
JSONObject json = XML.toJSONObject(inPara, true);

@looly looly closed this as completed Apr 25, 2024
@looly looly added the question label Apr 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants