PDA

View Full Version : QDomDocument Eats Up My Spaces



ManuMies
4th November 2010, 08:41
I'm using QDomDocument to create XML strings.

Expected result:


<container>
<datavalue> </datavalue>
</container>


Actual outcome:


<container>
<datavalue></datavalue>
</container>


But if my datavalue is like " 1 ", then it works. It's only happening when there are no characters at all. :eek:

Do you guys know any easy work around for it?

wysota
4th November 2010, 09:37
Whitespaces are transparent to xml, these are all equivalent:
<a> <b> </b> </a>
<a><b/></a>
<a>
<b>
<!-- multiple spaces here -->
</b>
</a>
If you want to preserve whitespaces you might try placing them in a CDATA section.

ManuMies
4th November 2010, 12:03
That's not really an option for me, since I need follow a format.

But, the easiest way is to first replace all the spaces with character X and then replace back after the QDomDocument is done and I'm getting it to a string?

wysota
4th November 2010, 12:23
That's not really an option for me, since I need follow a format.
Then you are not really using xml and QDomDocument might not be for you.


But, the easiest way is to first replace all the spaces with character X and then replace back after the QDomDocument is done and I'm getting it to a string?
That's probably a bad idea, you'll get a lot of X in places you won't be able to predict and won't be able to determine whether there was originally a space there or maybe a 'X' character.

tbscope
4th November 2010, 13:44
An XML parser should always present the data unchanged to the user, including white spaces.

http://www.w3.org/TR/2008/REC-xml-20081126/#sec-white-space

wysota
4th November 2010, 14:00
An XML parser should always present the data unchanged to the user, including white spaces.
Only if explicitly declared that white-spaces are to be preserved. In my opinion if you want an explicit whitespace, it's best to encode it as an entity, e.g. as &nbsp; or &# 32; (without the space) or something similar (%20?).

ChrisW67
4th November 2010, 22:21
An XML parser should always present the data unchanged to the user, including white spaces.

An XML processor MUST always pass all characters in a document that are not markup through to the application.
and


Definition: A software module called an XML processor is used to read XML documents and provide access to their content and structure.

This is an XML reader (XML processor in their terminology) requirement. Doesn't say anything about writers that I can find.

The collapsing of the space may also be the result of an explicit normalize() call.

Following from Wysota's post, adding the xml:space attribute to the elements may stop the loss of the space in the writer:


<container>
<datavalue xml:space="preserve"> </datavalue>
</container>
You may need to declare the attribute (see tbscope's link).

ManuMies
5th November 2010, 18:45
Thanks for the info, I'll try adding that attribute if it helps. :)

spawn9997
10th December 2010, 17:54
O.k. so did it help?