topical media & game development
basic-xml-11-AddNodes.htm / htm
<html>
<head>
<title>Add nodes.</title>
</head>
<body>
<script language="JScript" type="text/javascript">
try {
var strXML = "<?xml version='1.0' ?><Book><Chapter>This is Chapter 1.</Chapter><Chapter>This is Chapter 2.</Chapter><Chapter>This is Chapter 3.</Chapter></Book>"
var objXMLDOM = new ActiveXObject("Msxml2.DOMDocument.3.0");
objXMLDOM.loadXML(strXML);
alert(objXMLDOM.xml);
// First use the insertBefore(newChild, refChild) method to insert
// an Introduction element
objOriginalFirst = objXMLDOM.documentElement.firstChild;
var strNewText = "This is new text added using XML DOM programming.";
var objTextNode = objXMLDOM.createTextNode(strNewText);
var objToBeInserted = objXMLDOM.createElement("Introduction");
objToBeInserted.appendChild(objTextNode);
objXMLDOM.documentElement.insertBefore(objToBeInserted, objOriginalFirst);
alert(objXMLDOM.xml);
// Also add an Appendix element using appendChild() method
strNewText = "This is a new appendix added using XML DOM programming.";
objTextNode = objXMLDOM.createTextNode(strNewText);
var objToBeAdded = objXMLDOM.createElement("Appendix");
objToBeAdded.appendChild(objTextNode);
objXMLDOM.documentElement.appendChild(objToBeAdded);
alert(objXMLDOM.xml);
}
catch (e)
{
alert("An exception has occurred.");
}
</script>
</body>
</html>
(C) Æliens
20/2/2008
You may not copy or print any of this material without explicit permission of the author or the publisher.
In case of other copyright issues, contact the author.