Skip to main content
Inspiring
March 24, 2014
Question

XML toString method not working as expected.

  • March 24, 2014
  • 3 replies
  • 3035 views

Why is the XML.toString method inconsistant?

var xml:XML = new XML( "<root/>" );

var xml_withtext:XML = new XML( "<root>a</root>" );

var xml_withchild:XML = new XML( "<root><child/></root>" );

trace(xml.toString()); //traces "" (blank)
trace(xml_withtext.toString()); //traces "a"

trace(xml_withchild.toString()); //traces "<root><child/></root>"

If the XML contains only a root node, toString prints nothing.  When it contains text, it prints the text.  That would make sense if toString was printing only the contents of the root node, but if the root node contains a child, it doesn't print "<child/>" as one would expect.  Instead, it suddenly includes the root node as well in the string.  That is inconsistant/unexpected.  For some reason, there is also a separate toXMLString method that consistantly prints the entire XML structure.  Was that some kind of patch since toString doesn't work in a consistant manner, instead opting to sometimes include the root node depending on whether it contains simple or complex content?

This topic has been closed for replies.

3 replies

Participating Frequently
April 20, 2016

I agree that there is some inconsitency in the return data, where sometimes there's XML tags, and sometimes there's just text.  That's odd for a language like AS3 to have different return values where sometimes it's plain text and sometimes it's code.

But, that's what .toXMLString() is for, which seems pretty self-explanatory and even more descriptive than .toString()

Languages don't always get everything 100% right the first time, but when there's another function that does consistently return what you need, be happy that there's at least that because in AS2 it was a mess and largely devoid of "another function that does exactly what it should!"

Amy Blankenship
Legend
March 26, 2014

To add to what Andre1, another thing that toString() does is convert CDATA to a usable string. e4x is just a pain. You may find this link useful.

Inspiring
March 25, 2014

It works as promised: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/XML.html#toString()

The convenience of toString() returning particular node content becomes apparent when XML is parsed iteratively. Each node is an XML on its own merits. In other words node's children can be either XML or a simple content - toString() reflects this duality.

Inspiring
March 26, 2014

I understand how it works, already read the documentation, but as I stated it doesn't make sense for it return something different based on whether the node has "simple or complex" content.

XML is already a string, so a common method like "toString" when applied to string data should return the string.  Instead, the rather universal toString method returns something unintuitive, sometimes including the node's tag and sometimes not, depending on whether the node has child elements and whether those elements are text or another XML node.

I just think that the obviously special and exceptional functionality should have got the special name (e.g. toContentString) rather than having toString return something other than the xml string itself and instead adding a toXMLString method that actually just returns the existing string.  It's just strange, IMO.

Bottom line is, XML is a string, and most people would think that calling toString on an XML node would return the XML string, not "a blank string unless it contains child nodes, then it returns not the child node but suddenly the entire node".

Inspiring
March 26, 2014

1. It is what it is. Not all conventions are intuitive and we all know that there may be hundreds of reasons/dependencies why more descriptive or intuitive conventions are not chosen. We just have to deal with it.

2.

XML is already a string, so a common method like "toString" when applied to string data should return the string.

Strictly speaking, initial XML is not a String but an abstract text. It becomes/cast to String datatype when it is interpreted by consuming application IF this application has datatype String and when application's capacity to translate text into String is invoked. Before then XML is meaningless.

If content of file loaded or string written has a character positioning pattern that meets requirements of XML conventions (which is just an idea) - it is defined as something that can be named/interpreted as XML. One very well can name file with tags something different than XML - for example HTML or just text.

AS3 XML object is not a String - it is datatype XML.

3.

Instead, the rather universal toString method returns something unintuitive, sometimes including the node's tag and sometimes not, depending on whether the node has child elements and whether those elements are text or another XML node.

a. There is no universal toString() method that does everything exactly the same with all datatypes and contexts. In the instance of XML class, it is obvious that for one reason or another it was found optimal to override Object.toString() to accommodate particular logic.

This brings me back to the point 1 - since toString() is overridden - it is plain efficient not to create another API entity.

b. All XML elements are children of the corresponding parent elements no matter their structure. From this perspective there is no any ambiguity in XML.toString() implementation. I personally like it like that because XML structure and individual element meaning is fluid and unpredictable. Thus - toString() offers a bridge from an abstract idea of XML element child to particular implementation.

4.

... then it returns not the child node but suddenly the entire node".

toString() is not returning AS3 XML object but just an AS3 (ECMA) String. This means that value obtained via calling XML.toString() looses all XML object related functionality. In some cases this can be a very desirable outcome.

In other words toString() does not return XML at all but an un-interpreted representation that is cast to String.

It is worth mentioning again that toString() returns specific to AS3 String datatype. This special datatype may have nothing in common with the programmatic concept of string. In other languages datatype string can be a totally different animal.

5.

Bottom line is, XML is a string, and most people would think that calling toString on an XML node would return the XML string, not "a blank string unless it contains child nodes, then it returns not the child node but suddenly the entire node".

Again, XML is not a string until application says so and XML in AS3 is not a String but a special object.