Entry
How do I use the method toString?
How do I use the method toString?
Apr 17th, 2005 01:37
Martin Honnen,
The method toString takes no argument and returns a convenient string
representation of the XMLList. If you want the serialization of all XML
objects in the XMLList then do not use toString but use toXMLString instead.
Note that the result of toString depends on whether the XMLList has
simple content or not, if it has simple content (thus if it contains no
XML element object at all or if it contains exactly one XML element
object with simple content) then to toString representation of all XML
objects in the XMLList besides comments and processing instructions is
concatenated. If the XMLList how does not have simple content then
toXMLString is called.
Here are some examples:
var textList = new XMLList();
for (var i = 0; i < 3; i++) {
textList += new XML('Text ' + i + '.');
}
// implicit call of toString method
alert(textList); // shows 'Text 0.Text 1.Text 2.'
// explicit call of toString method has same result
alert(textList.toString()) // shows 'Text 0.Text 1.Text 2.'
// compare toXMLString result
alert(textList.toXMLString());
/* shows
'Text 0.
Text 1.
Text 2.'
*/
var simpleList = <>
<god>Kibo</god>
</>;
alert(simpleList); // shows 'Kibo'
// compare toXMLString result
alert(simpleList.toXMLString()); // shows '<god>Kibo</god>'
var twoElementList = <>
<god>Kibo</god>
<god>Xibo</god>
</>;
alert(twoElementList);
/* shows
'<god>Kibo</god>
<god>Xibo</god>'
*/
// no difference here to toXMLString:
alert(twoElementList.toXMLString());