Thursday, February 5, 2009

Whitespace frustrations


Update:
So, I got it to work eventually. Apparently if I also set this property:
XML.prettyPrinting = false;
I get the expected results. Not pretty at all, but at least it solved my problem for now...


I'm making this little Flex proof-of-technique application currently that deals with quite a bit of XML.
One of the first things I encountered was that an element looking like this:

<node>text with trailing whitespace </node>


The text of this node ended up in my object as 'text with trailing whitespace'. So *without* the trailing space, which was essential to DO end up there.

Well, a quick google turned out that before I parse my incoming XML I should add this line of code:

XML.ignoreWhitespace = false;

And then create my XML object.

On a side note, I think its kind of weird to set such a parameter through a static property. In my opinion it would have made more sense to just add a constructor argument which defaults to true, sort of like this:


var ignorewhitespace:Boolean = false;
var xmlObject:XML = new XML(BigFrakkingXMLString,ignorewhitespace);


But, unfortunately my problems weren't over.
On my way back to the server I'm supposed to create an XML object and send this.
So, what do I do? I set the static property: XML.ignoreWhitespace = false;
And I start creating elements and adding them to my XML object.
For example, I add an element like this:

var contentXML:XML = new XML("<Content>" + someStringOfText + "</Content>");

where the someStringOfText string has a value of, for example, "some text with a trailing space again ".

Yet, somehow the element ends up looking like this:

<Content>some text with a trailing space again</Content>


So, am I dealing with a bug here, or am I doing something fundamentally wrong here again?

I guess an option would be to just send value objects back to the server and handle all of the XML creation there. But I'm sort of buggered by the fact that I seem to hit a wall here over such a small little issue...

No comments:

Post a Comment

About Me

My photo

Co-Owner at Stack & Heap (http://www.stackandheap.com)
Lead developer for the Spring Actionscript (http://www.springactionscript.org) and AS3Commons (http://www.as3commons.org) projects.
I like noise.