Skip to main content

Preserving whitespace

4 replies [Last post]
nickminutello
Offline
Joined: 2003-08-09

Is there a simple way to get JAXB to preserve whitespace in strings in attributes/elements?

Or is it a matter of plugging in your own ContentHandler ala https://jaxb.dev.java.net/faq/JaxbCDATASample.java?
(Is there a way of doing that without depending on Xerces?)

Cheers,
Nick

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
kohsuke
Offline
Joined: 2003-06-09

whitespaces in attribues are normalized by XML parsers, as specified in the XML recommendation. Some parsers might have an option to disable this, but I don't know.

Non-significant whitespaces in elements are ignored by JAXB unmarshaller (such as those whitespaces inside elements that only have child elements and no PCDATA), since JAXB POJO objects don't have any data fields to keep those whitespaces, there really isn't any way to preserve them.

Significant whitespaces in elements, such as those whitespaces that match XML Schema datatypes (like, say, foo bar ) are processed according to the datatype whitespace normalization rules. For example, whitespaces will be ignored for datatype like xs:int, xs:QName, xs:token, etc. The only datatype where whitespace is preserved is xs:string.

nickminutello
Offline
Joined: 2003-08-09

> whitespaces in attribues are normalized by XML
> parsers, as specified in the XML recommendation. Some
> parsers might have an option to disable this, but I
> don't know.
Yep, Agreed.

> The only datatype where whitespace is preserved is
> xs:string.
We were seeing CRLF and Tabs removed when marshalling string fields...

-Nick

kohsuke
Offline
Joined: 2003-06-09

If that's the case, then that is a bug.
If you can file an issue with a test case to http://jaxb.dev.java.net/issues/ , that would be really great.

nitesh_123
Offline
Joined: 2013-09-06

kohsuke wrote:
If that's the case, then that is a bug.
If you can file an issue with a test case to http://jaxb.dev.java.net/issues/ , that would be really great.

I am also facing the same issue. I am setting blank character in field but it is not preserving in response xml.

java code for setting field:

if (populateCustomerType)
{
unfCustomerResponse.setUnfCustomerType("98");
}
else
{
unfCustomerResponse.setUnfCustomerType(" ");
}

XML response:

please do reply.its urgent