JAXB: How to keep consecutive spaces as they are in source XML during unmarshalling

梦想的初衷 提交于 2019-12-11 22:29:06

问题


I am using JAXB to unmarshall an XML message. It seems to replace multiple consecutive spaces by a single space.

<testfield>this is a       test<\testfield>

(several spaces between a and test)

upon unmarshalling, the above becomes:

this is test

How do I keep consecutive spaces as they are in source XML?


回答1:


From the msdn page:

Document authors can use the xml:space attribute to identify portions of documents where white space is considered important. Style sheets can also use the xml:space attribute as a hook to preserve white space in presentation. However, because many XML applications do not understand the xml:space attribute, its use is considered advisory.

You can try adding xml:space="preserve" so it doesn't replace the spaces

<poem xml:space="default">
<author xml:space="default">
<givenName xml:space="default">Alix</givenName>
<familyName xml:space="default">Krakowski</familyName>
</author>
<verse xml:space="preserve">
<line xml:space="preserve">Roses   are  red,</line>
<line xml:space="preserve">Violets  are  blue.</line>
<signature xml:space="default">-Alix</signature>
</verse>
</poem>

http://msdn.microsoft.com/en-us/library/ms256097%28v=vs.110%29.aspx



来源:https://stackoverflow.com/questions/22462300/jaxb-how-to-keep-consecutive-spaces-as-they-are-in-source-xml-during-unmarshall

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