↑
Main Page
XML literals
</employee>
</employees>;
oXml.appendChild(<employee position=”Vice President”>
<name>Benjamin Anderson</name>
</employee>);
oXml.prependChild(<employee position=”User Interface Designer”>
<name>Michael Johnson</name>
</employee>);
oXml.insertChildBefore(oXml.child(2), <employee position=”Human Resources Manager”>
<name>Margaret Jones</name>
</employee>);
oXml.setChildren(<employee position=”President”>
<name>Richard McMichael</name>
</employee> +
<employee position=”Vice President”>
<name>Rebecca Smith</name>
</employee>);
This code illustrates some of the methods discussed previously. Note that you can use XML literals in
place of
XML
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
XMLList
object. The resulting XML looks like this:
<employees>
<employee position=”President”>
<name>Richard McMichael</name>
</employee>
<employee position=”Vice President”>
<name>Rebecca Smith</name>
</employee>
</employees>
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:
hasComplexContent()
and
hasSimpleContent()
. 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
true
for any
given element.
var oSimpleXml = <message>Hello World!</message>;
var oComplexXML =
<message><greeting>Hello</greeting><target>World!</target></message>
var bSimple = oSimpleXml.hasSimpleContent(); //returns true
var bComplex = oComplexXML.hasComplexContent(); //returns true
612
Chapter 20
23_579088 ch20.qxd 3/28/05 11:44 AM Page 612
Free JavaScript Editor
Ajax Editor
©
→