JAXB - 编组单引号字符代码
我正在使用 JAXB 生成上传到我们的 Google feed 的 XML。在测试和比较这个新方法的输出与我们执行此操作的旧方法(使用 JSP)的输出时,我注意到单引号字符没有得到正确处理。
字段内容:
& ' " > <
旧的正确输出:
<title> & ' " > < </title>
新的不正确的输出:
<title> & ' " > < </title>
在编组 XML 之前,我尝试将字段中的所有单引号字符替换为
'
,但这最终会用其字符代码替换 & 符号,并留下一个无用的 #39 坐姿编组后在那里。
我应该在什么时候尝试解决这个问题?在将字符串传递到 JAXB 类之前,我是否可以通过以某种方式更改字符串来获得正确的行为,或者我是否必须执行某些操作来更改编组处理单引号的方式?
感谢您的阅读!
编辑: 抱歉,我之前不太清楚,Google 的文档要求这 5 个字符由其实体或字符代码表示。
来自他们的文档:
不在 CDATA 部分中的数据值(包括 URL)必须使用转义码来表示在 下表。您可以使用实体代码或字符代码来表示这些特殊字符。
Ampersand & & &
Single Quote ' ' '
Double Quote " " "
Greater Than > > >
Less Than < < <
如果可能的话,我想避免 CDATA 路线。
I'm using JAXB to generate XML that is uploaded to our Google feed. While testing and comparing this new method's output to the output from the old way that we were doing it (Using JSPs), I noticed that single quote characters aren't being handled correctly.
Field Content:
& ' " > <
Old Correct output:
<title> & ' " > < </title>
New Incorrect output:
<title> & ' " > < </title>
I tried replacing all single quote characters in the field with
'
before I marshall the XML, but this ends up replacing the ampersand with its character code as well as leaving me with a useless #39 sitting there after marshalling.
At which point should I try to remedy this problem? Can I get the correct behavior by altering the string in some way before passing it into the JAXB class, or is there something I must do to change how the marshalling handles single quotes?
Thanks for reading!
EDIT:
Sorry I wasn't more clear before, Google's documentation requires that those 5 characters are represented either by their Entity or Character Codes.
From their documentation:
Data values that are not in CDATA sections, including URLs, must use escape codes for the characters listed in the
following table. You can use either the entity code or the character code to represent these special characters.
Ampersand & & &
Single Quote ' ' '
Double Quote " " "
Greater Than > > >
Less Than < < <
I would like to avoid the CDATA route if possible.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
单引号不必转义。第二个输出在 XML 格式方面是正确的,并且更简洁,这是更好的。
如果您想精细控制转义哪些字符(以及如何转义),您可以尝试实现自己的
CharacterEscapeHandle
。从未尝试过,但它被记录为 JSXB RI 的一项功能。另请参阅:
Single quotes don't have to be escaped. The second output is correct with regards to XML format and more concise, which is even better.
If you want fine-grained control over which characters are escaped (and how), you might try to implement your own
CharacterEscapeHandle
. Never tried it, but it is documented as a feature of JSXB RI.See also: