faqts : Computers : Programming : Languages : JavaScript : XML : E4X (ECMAScript for XML) : XML objects

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

1 of 1 people (100%) answered Yes
Recently 1 of 1 people (100%) answered Yes

Entry

How do I use the method toString?
How do I use the method toString?

Apr 12th, 2005 04:06
Martin Honnen,


The method toString takes no argument and returns a convenient string
value of this XML object. It is usually not necessary to call the method
explictly, rather it is applied automatically when an XML object is used
in a string context.
Note that the method does not necessarily serialize the complete XML
object (respectively return the XML markup of the object), if you need
that use the method toXMLString.
The convenient string value returned depends on the node kind of the object:
  attribute node:
    attribute value is returned
  text node:
    text content of the text node is returned
  element node:
    if the element node has simple content then the text content
    of the element is returned
    otherwise element.toXMLString() is returned
  comment node:
    toXMLString() is returned
  processing instruction node:
    toXMLString() is returned
Here are some examples:
  var god = <god>Kibo</god>;
  alert(god); 
  // shows 'Kibo', note that toString() is called here automatically
  alert(god.toString()); // shows 'Kibo' too
  var p = <p>Kibology for <b>all</b>. All for <b>Kibology</b>.</p>;
  alert(p);
  /* element does not have simple content, therefore 
     toString() calls toXMLString() and thus alert shows 
  '<p>
     Kibology for
     <b>all</b>
     . All for
     <b>Kibology</b>
     .
   </p>'
  */
  var god = <god name="Kibo" />;
  alert(god.@name); // shows 'Kibo'
  var text = new XML("Kibology for all.");
  alert(text); // shows 'Kibology for all.'
  XML.ignoreComments = false;
  XML.ignoreProcessingInstructions = false;
  var comment = <!-- Kibology for all. -->;
  alert(comment); // shows '<!-- Kibology for all. -->'
  var processingInstruction = <?process Kibology="on"?>;
  alert(processingInstruction); // shows '<?process Kibology="on"?>'