问题
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