Main Page

XML literals

oXml.appendChild(<employee position=”Vice President”>
<name>Benjamin Anderson</name>
oXml.prependChild(<employee position=”User Interface Designer”>
<name>Michael Johnson</name>
oXml.insertChildBefore(oXml.child(2), <employee position=”Human Resources Manager”>
<name>Margaret Jones</name>
oXml.setChildren(<employee position=”President”>
<name>Richard McMichael</name>
</employee> +
<employee position=”Vice President”>
<name>Rebecca Smith</name>
This code illustrates some of the methods discussed previously. Note that you can use XML literals in
place of
objects in all methods. First, the code adds a Vice President named Benjamin Anderson to
the bottom of the list of employees. Second, a User Interface Designer named Michael Johnson is added
to the top of the list of employees. Third, A Human Resources Manager named Margaret Jones is added
just before the employee in position 2, which at this point is Jim Smith (because Michael Johnson and
Nicholas C. Zakas now come before him). Finally, all the children are replaced with President Richard
McMichael and Vice President Rebecca Smith (maybe there was a major layoff). Note, in that line, the
plus symbol between the two employee literals. This indicates that the values are contained in an
object. The resulting XML looks like this:
<employee position=”President”>
<name>Richard McMichael</name>
<employee position=”Vice President”>
<name>Rebecca Smith</name>
There has been a lot of talk in this section about the important concepts of simple and complex content.
They are so important, in fact, that the XML object has methods to help out:
. Each of these methods returns a Boolean value indicating whether the ele-
ment contains the type of content indicated. By rule, only one of these methods can return
for any
given element.
var oSimpleXml = <message>Hello World!</message>;
var oComplexXML =
var bSimple = oSimpleXml.hasSimpleContent(); //returns true
var bComplex = oComplexXML.hasComplexContent(); //returns true
Chapter 20
23_579088 ch20.qxd 3/28/05 11:44 AM Page 612

JavaScript EditorFree JavaScript Editor     Ajax Editor