04.11.2015 Views

javascript

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 16: ECMA Script for XML<br />

Aside from this basic XML construction syntax, the following DOM - like methods are also available:<br />

❑<br />

❑<br />

❑<br />

❑<br />

❑<br />

❑<br />

❑<br />

appendChild( child ) — Appends the given child to the end of the XMLList representing the<br />

node ’ s children<br />

copy() — Returns a duplicate of the XML object<br />

insertChildAfter( refNode, child ) — Inserts child after refNode in the XMLList<br />

representing the node ’ s children<br />

insertChildBefore( refNode, child ) — Inserts child before refNode in the XMLList<br />

representing the node ’ s children<br />

prependChild( child ) — Inserts the given child at the beginning of the XMLList<br />

representing the node ’ s children<br />

replace( propertyName, value ) — Replaces the property named propertyName , which<br />

may be an element or an attribute, with the given value<br />

setChildren( children ) — Replaces all current children with children , which may be an<br />

XML object or an XMLList object<br />

These methods are incredibly useful and easy to use. The following code illustrates some of these<br />

methods:<br />

var employees = < employees ><br />

< employee position=”Software Engineer” ><br />

< name > Nicholas C. Zakas < /name ><br />

< /employee ><br />

< employee position=”Salesperson” ><br />

< name > Jim Smith < /name ><br />

< /employee ><br />

< /employees > ;<br />

employees.appendChild( < employee position=”Vice President” ><br />

< name > Benjamin Anderson < /name ><br />

< /employee > );<br />

employees.prependChild( < employee position=”User Interface Designer” ><br />

< name > Michael Johnson < /name ><br />

< /employee > );<br />

employees.insertChildBefore(employees.child(2),<br />

< employee position=”Human Resources Manager” ><br />

< name > Margaret Jones < /name ><br />

< /employee > );<br />

employees.setChildren( < employee position=”President” ><br />

< name > Richard McMichael < /name ><br />

< /employee > +<br />

< employee position=”Vice President” ><br />

< name > Rebecca Smith < /name ><br />

< /employee > );<br />

559

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!