mohd130
asked on
Add, Update and Delete XML nodes - JavaScript
I’m wondering if it’s possible to:
Add, Update and Delete nodes from an existing XML file, using JavaScript on Client side (No Server)
If you can provide me with full example I will double the points.
Mo
Add, Update and Delete nodes from an existing XML file, using JavaScript on Client side (No Server)
If you can provide me with full example I will double the points.
Mo
The XML Created Is (copy and save this as data.xml)
===================
<?xml version="1.0" encoding="iso-8859-1"?>
<payload timestamp="2002-05-12T22:0 1:54" users="1034">
<section category="food">
<item name="Burmese Curry" price="USD10"/>
</section>
<section category="drinks" type="non-alcoholic">
<subitem name="Super Water" price="USD200"/>
</section>
</payload>
The HTML (which should be opened locally again)
========================== ========== ========== ==
<html>
<head>
<title>XMLDOM Manipulation</title>
<script language="javascript">
var oXML = new ActiveXObject("Microsoft.X MLDOM");
var oXSLT = new ActiveXObject("Microsoft.X MLDOM");
var wXML;
oXML.async = false;
oXML.load("data.xml");
oXSLT.async = true;
oXSLT.load("res://msxml.dl l/defaults s.xsl");
/*****BEGINNING OF GENERIC SCRIPTS****/
/*****COPYRIGHT BY BRANDON DRIESEN****/
//IXMLDOMElement AddElementDOM(IXMLDOMEleme nt oParent,string szName, [string szText])
function AddElementDOM(oParent,szNa me,szText) {
var oDOM = oParent.ownerDocument;
var newElement = oDOM.createElement(szName) ;
if ("string" == typeof(szText) && szText.length != 0) newElement.text = szText;
oParent.appendChild(newEle ment);
return newElement;
}
//void AddAttributes(IXMLDOMEleme nt oNode, variant[] vtList);
function AddAttributesDOM(oNode,vtL ist) {
if (oNode.nodeType != 1) return;
for (var i=0;i<vtList.length;i+=2) {
oNode.setAttribute(vtList[ i],vtList[ i+1]);
}
}
//void RemoveAttributesDOM(IXMLDO MElement variant[] vtList);
function RemoveAttributesDOM(oNode, vtList) {
if (oNode.nodeType != 1) return;
for (var i=0;i<vtList.length;i++) {
oNode.removeAttribute(vtLi st[i]);
}
}
//void RemoveElementDOM(IXMLDOMEl ement oNode)
function RemoveElementDOM(oNode) {
var oParent = oNode.parentNode;
oParent.removeChild(oNode) ;
}
//IXMLDOMElement SetRootElementDOM(DOMDocum ent oDOM, string szName, [string szEncoding]
function SetRootElementDOM(oDOM,szN ame,szEnco ding) {
szEncoding = "string" == typeof(szEncoding) && szEncoding.length != 0 ? " encoding='" + szEncoding + "'" : "";
StripRootElementsDOM(oDOM) ;
oDOM.appendChild(oDOM.crea teProcessi ngInstruct ion("xml", "version=' 1.0'" + szEncoding));
oDOM.appendChild(oDOM.crea teElement( szName));
return oDOM.documentElement;
}
//void StripRootElementsDOM(DOMDo cument oDOM)
function StripRootElementsDOM(oDOM) {
for (var i=0; i<oDOM.childNodes.length;i ++) {
if (oDOM.childNodes.item(i).n odeType != 1) {
oDOM.removeChild(oDOM.chil dNodes.ite m(i));
}
}
if (oDOM.documentElement) oDOM.removeChild(oDOM.docu mentElemen t);
}
window.onload = init;
function init() {
//removal of element
var oSection = [];
oSection[0] = oXML.selectSingleNode("//s ection[@ca tegory='dr inks']");
if (oSection[0]) RemoveElementDOM(oSection[ 0]);
//adding a new element with attributes to food section
oSection[1] = oXML.selectSingleNode("//s ection[@ca tegory='fo od']");
if (oSection[1]) {
AddAttributesDOM(AddElemen tDOM(oSect ion[1],"it em"),["nam e","Fish & Chips","price","USD200"]);
AddAttributesDOM(AddElemen tDOM(oSect ion[1],"it em"),["nam e","Hotdog ","price", "USD100"]) ;
//get reference to new element created
var oFood = AddElementDOM(oSection[1], "item");
//add attributes to food
AddAttributesDOM(oFood,["n ame","Stea k","price" ,"USD200"] );
//add elements and attributes to food
AddAttributesDOM(AddElemen tDOM(oFood ,"locality "),["name" ,"North"," discount", 0.1]);
AddAttributesDOM(AddElemen tDOM(oFood ,"locality "),["name" ,"South"," discount", 0.15]);
AddAttributesDOM(AddElemen tDOM(oFood ,"locality "),["name" ,"West","d iscount",0 .07]);
}
//change attributes
AddAttributesDOM(oXML.docu mentElemen t,["timest amp",getIS ODateTime( new Date()),"data","true"]);
RemoveAttributesDOM(oXML.d ocumentEle ment,["use rs"]);
DisplayXML();
}
function getISODateTime(datItem) {
var szDate = datItem.getYear() + "-";
szDate += (datItem.getMonth() < 9 ? "0" : "") + (datItem.getMonth() + 1) + "-";
szDate += (datItem.getDate() < 10 ? "0" : "") + datItem.getDate() + "T";
szDate += (datItem.getHours() < 10 ? "0" : "") + datItem.getHours() + ":";
szDate += (datItem.getMinutes() < 10 ? "0" : "") + datItem.getMinutes() + ":";
szDate += (datItem.getSeconds() < 10 ? "0" : "") + datItem.getSeconds();
return szDate;
}
function DisplayXML() {
if (oXSLT.readyState != 4) {
window.setTimeout("Display XML()",100 );
} else {
if (wXML != null && "object" == typeof(wXML.opener) && wXML.opener != null) wXML.close();
wXML = window.open("about:blank", "myXMLData ");
wXML.document.write(oXML.t ransformNo de(oXSLT)) ;
wXML.document.close();
}
}
</script>
</head>
<body></body>
</html>
This is the manipulated XML Tree
========================== ======
<?xml version="1.0" encoding="iso-8859-1"?>
<payload timestamp="2002-05-12T22:2 5:38" data="true">
<section category="food">
<item name="Burmese Curry" price="USD10"/>
<item name="Fish & Chips" price="USD200"/>
<item name="Hotdog" price="USD100"/>
<item name="Steak" price="USD200">
<locality name="North" discount="0.1"/>
<locality name="South" discount="0.15"/>
<locality name="West" discount="0.07"/>
</item>
</section>
</payload>
Regards,
Brandon Driesen
===================
<?xml version="1.0" encoding="iso-8859-1"?>
<payload timestamp="2002-05-12T22:0
<section category="food">
<item name="Burmese Curry" price="USD10"/>
</section>
<section category="drinks" type="non-alcoholic">
<subitem name="Super Water" price="USD200"/>
</section>
</payload>
The HTML (which should be opened locally again)
==========================
<html>
<head>
<title>XMLDOM Manipulation</title>
<script language="javascript">
var oXML = new ActiveXObject("Microsoft.X
var oXSLT = new ActiveXObject("Microsoft.X
var wXML;
oXML.async = false;
oXML.load("data.xml");
oXSLT.async = true;
oXSLT.load("res://msxml.dl
/*****BEGINNING OF GENERIC SCRIPTS****/
/*****COPYRIGHT BY BRANDON DRIESEN****/
//IXMLDOMElement AddElementDOM(IXMLDOMEleme
function AddElementDOM(oParent,szNa
var oDOM = oParent.ownerDocument;
var newElement = oDOM.createElement(szName)
if ("string" == typeof(szText) && szText.length != 0) newElement.text = szText;
oParent.appendChild(newEle
return newElement;
}
//void AddAttributes(IXMLDOMEleme
function AddAttributesDOM(oNode,vtL
if (oNode.nodeType != 1) return;
for (var i=0;i<vtList.length;i+=2) {
oNode.setAttribute(vtList[
}
}
//void RemoveAttributesDOM(IXMLDO
function RemoveAttributesDOM(oNode,
if (oNode.nodeType != 1) return;
for (var i=0;i<vtList.length;i++) {
oNode.removeAttribute(vtLi
}
}
//void RemoveElementDOM(IXMLDOMEl
function RemoveElementDOM(oNode) {
var oParent = oNode.parentNode;
oParent.removeChild(oNode)
}
//IXMLDOMElement SetRootElementDOM(DOMDocum
function SetRootElementDOM(oDOM,szN
szEncoding = "string" == typeof(szEncoding) && szEncoding.length != 0 ? " encoding='" + szEncoding + "'" : "";
StripRootElementsDOM(oDOM)
oDOM.appendChild(oDOM.crea
oDOM.appendChild(oDOM.crea
return oDOM.documentElement;
}
//void StripRootElementsDOM(DOMDo
function StripRootElementsDOM(oDOM)
for (var i=0; i<oDOM.childNodes.length;i
if (oDOM.childNodes.item(i).n
oDOM.removeChild(oDOM.chil
}
}
if (oDOM.documentElement) oDOM.removeChild(oDOM.docu
}
window.onload = init;
function init() {
//removal of element
var oSection = [];
oSection[0] = oXML.selectSingleNode("//s
if (oSection[0]) RemoveElementDOM(oSection[
//adding a new element with attributes to food section
oSection[1] = oXML.selectSingleNode("//s
if (oSection[1]) {
AddAttributesDOM(AddElemen
AddAttributesDOM(AddElemen
//get reference to new element created
var oFood = AddElementDOM(oSection[1],
//add attributes to food
AddAttributesDOM(oFood,["n
//add elements and attributes to food
AddAttributesDOM(AddElemen
AddAttributesDOM(AddElemen
AddAttributesDOM(AddElemen
}
//change attributes
AddAttributesDOM(oXML.docu
RemoveAttributesDOM(oXML.d
DisplayXML();
}
function getISODateTime(datItem) {
var szDate = datItem.getYear() + "-";
szDate += (datItem.getMonth() < 9 ? "0" : "") + (datItem.getMonth() + 1) + "-";
szDate += (datItem.getDate() < 10 ? "0" : "") + datItem.getDate() + "T";
szDate += (datItem.getHours() < 10 ? "0" : "") + datItem.getHours() + ":";
szDate += (datItem.getMinutes() < 10 ? "0" : "") + datItem.getMinutes() + ":";
szDate += (datItem.getSeconds() < 10 ? "0" : "") + datItem.getSeconds();
return szDate;
}
function DisplayXML() {
if (oXSLT.readyState != 4) {
window.setTimeout("Display
} else {
if (wXML != null && "object" == typeof(wXML.opener) && wXML.opener != null) wXML.close();
wXML = window.open("about:blank",
wXML.document.write(oXML.t
wXML.document.close();
}
}
</script>
</head>
<body></body>
</html>
This is the manipulated XML Tree
==========================
<?xml version="1.0" encoding="iso-8859-1"?>
<payload timestamp="2002-05-12T22:2
<section category="food">
<item name="Burmese Curry" price="USD10"/>
<item name="Fish & Chips" price="USD200"/>
<item name="Hotdog" price="USD100"/>
<item name="Steak" price="USD200">
<locality name="North" discount="0.1"/>
<locality name="South" discount="0.15"/>
<locality name="West" discount="0.07"/>
</item>
</section>
</payload>
Regards,
Brandon Driesen
ASKER
Many Thanks Brandon
I'm sure you have the html file which call each function on the script, please if you can copy it here, because I found that it’s very difficult to me to understand each function without the parameter.
Mo
I'm sure you have the html file which call each function on the script, please if you can copy it here, because I found that it’s very difficult to me to understand each function without the parameter.
Mo
I will make it easier for you:
STEP 1
=======
Please copy and paste the following in notepad and save it as data.xml (not data.xml.txt!!!)
XML Document (data.xml)
=======================
<?xml version="1.0" encoding="iso-8859-1"?>
<payload timestamp="2002-05-12T22:0 1:54" users="1034">
<section category="food">
<item name="Burmese Curry" price="USD10"/>
</section>
<section category="drinks" type="non-alcoholic">
<subitem name="Super Water" price="USD200"/>
</section>
</payload>
STEP 1
=======
Please copy and paste the following in notepad and save it as data.xml (not data.xml.txt!!!)
XML Document (data.xml)
=======================
<?xml version="1.0" encoding="iso-8859-1"?>
<payload timestamp="2002-05-12T22:0
<section category="food">
<item name="Burmese Curry" price="USD10"/>
</section>
<section category="drinks" type="non-alcoholic">
<subitem name="Super Water" price="USD200"/>
</section>
</payload>
STEP 2
=======
Please copy and paste the following in notepad and save it as data.html and in the same folder/location as data.xml
HTML Document (data.html)
=========================
<html>
<head>
<title>XMLDOM Manipulation</title>
<script language="javascript">
var oXML = new ActiveXObject("Microsoft.X MLDOM");
var oXSLT = new ActiveXObject("Microsoft.X MLDOM");
var wXML;
oXML.async = false;
oXML.load("data.xml");
oXSLT.async = true;
oXSLT.load("res://msxml.dl l/defaults s.xsl");
/*****BEGINNING OF GENERIC SCRIPTS****/
/*****COPYRIGHT BY BRANDON DRIESEN****/
//IXMLDOMElement AddElementDOM(IXMLDOMEleme nt oParent,string szName, [string szText])
function AddElementDOM(oParent,szNa me,szText) {
var oDOM = oParent.ownerDocument;
var newElement = oDOM.createElement(szName) ;
if ("string" == typeof(szText) && szText.length != 0) newElement.text = szText;
oParent.appendChild(newEle ment);
return newElement;
}
//void AddAttributes(IXMLDOMEleme nt oNode, variant[] vtList);
function AddAttributesDOM(oNode,vtL ist) {
if (oNode.nodeType != 1) return;
for (var i=0;i<vtList.length;i+=2) {
oNode.setAttribute(vtList[ i],vtList[ i+1]);
}
}
//void RemoveAttributesDOM(IXMLDO MElement variant[] vtList);
function RemoveAttributesDOM(oNode, vtList) {
if (oNode.nodeType != 1) return;
for (var i=0;i<vtList.length;i++) {
oNode.removeAttribute(vtLi st[i]);
}
}
//void RemoveElementDOM(IXMLDOMEl ement oNode)
function RemoveElementDOM(oNode) {
var oParent = oNode.parentNode;
oParent.removeChild(oNode) ;
}
//IXMLDOMElement SetRootElementDOM(DOMDocum ent oDOM, string szName, [string szEncoding]
function SetRootElementDOM(oDOM,szN ame,szEnco ding) {
szEncoding = "string" == typeof(szEncoding) && szEncoding.length != 0 ? " encoding='" + szEncoding
+ "'" : "";
StripRootElementsDOM(oDOM) ;
oDOM.appendChild(oDOM.crea teProcessi ngInstruct ion("xml", "version=' 1.0'" + szEncoding));
oDOM.appendChild(oDOM.crea teElement( szName));
return oDOM.documentElement;
}
//void StripRootElementsDOM(DOMDo cument oDOM)
function StripRootElementsDOM(oDOM) {
for (var i=0; i<oDOM.childNodes.length;i ++) {
if (oDOM.childNodes.item(i).n odeType != 1) {
oDOM.removeChild(oDOM.chil dNodes.ite m(i));
}
}
if (oDOM.documentElement) oDOM.removeChild(oDOM.docu mentElemen t);
}
window.onload = init;
function init() {
//removal of element
var oSection = [];
oSection[0] = oXML.selectSingleNode("//s ection[@ca tegory='dr inks']");
if (oSection[0]) RemoveElementDOM(oSection[ 0]);
//adding a new element with attributes to food section
oSection[1] = oXML.selectSingleNode("//s ection[@ca tegory='fo od']");
if (oSection[1]) {
AddAttributesDOM(AddElemen tDOM(oSect ion[1],"it em"),["nam e","Fish & Chips","price","USD200"]);
AddAttributesDOM(AddElemen tDOM(oSect ion[1],"it em"),["nam e","Hotdog ","price", "USD100"]) ;
//get reference to new element created
var oFood = AddElementDOM(oSection[1], "item");
//add attributes to food
AddAttributesDOM(oFood,["n ame","Stea k","price" ,"USD200"] );
//add elements and attributes to food
AddAttributesDOM(AddElemen tDOM(oFood ,"locality "),["name" ,"North"," discount", 0.1]);
AddAttributesDOM(AddElemen tDOM(oFood ,"locality "),["name" ,"South"," discount", 0.15]);
AddAttributesDOM(AddElemen tDOM(oFood ,"locality "),["name" ,"West","d iscount",0 .07]);
}
//change attributes
AddAttributesDOM(oXML.docu mentElemen t,["timest amp",getIS ODateTime( new Date()),"data","true"]);
RemoveAttributesDOM(oXML.d ocumentEle ment,["use rs"]);
DisplayXML();
}
function getISODateTime(datItem) {
var szDate = datItem.getYear() + "-";
szDate += (datItem.getMonth() < 9 ? "0" : "") + (datItem.getMonth() + 1) + "-";
szDate += (datItem.getDate() < 10 ? "0" : "") + datItem.getDate() + "T";
szDate += (datItem.getHours() < 10 ? "0" : "") + datItem.getHours() + ":";
szDate += (datItem.getMinutes() < 10 ? "0" : "") + datItem.getMinutes() + ":";
szDate += (datItem.getSeconds() < 10 ? "0" : "") + datItem.getSeconds();
return szDate;
}
function DisplayXML() {
if (oXSLT.readyState != 4) {
window.setTimeout("Display XML()",100 );
} else {
if (wXML != null && "object" == typeof(wXML.opener) && wXML.opener != null) wXML.close();
wXML = window.open("about:blank", "myXMLData ");
wXML.document.write(oXML.t ransformNo de(oXSLT)) ;
wXML.document.close();
}
}
</script>
</head>
<body></body>
</html>
=======
Please copy and paste the following in notepad and save it as data.html and in the same folder/location as data.xml
HTML Document (data.html)
=========================
<html>
<head>
<title>XMLDOM Manipulation</title>
<script language="javascript">
var oXML = new ActiveXObject("Microsoft.X
var oXSLT = new ActiveXObject("Microsoft.X
var wXML;
oXML.async = false;
oXML.load("data.xml");
oXSLT.async = true;
oXSLT.load("res://msxml.dl
/*****BEGINNING OF GENERIC SCRIPTS****/
/*****COPYRIGHT BY BRANDON DRIESEN****/
//IXMLDOMElement AddElementDOM(IXMLDOMEleme
function AddElementDOM(oParent,szNa
var oDOM = oParent.ownerDocument;
var newElement = oDOM.createElement(szName)
if ("string" == typeof(szText) && szText.length != 0) newElement.text = szText;
oParent.appendChild(newEle
return newElement;
}
//void AddAttributes(IXMLDOMEleme
function AddAttributesDOM(oNode,vtL
if (oNode.nodeType != 1) return;
for (var i=0;i<vtList.length;i+=2) {
oNode.setAttribute(vtList[
}
}
//void RemoveAttributesDOM(IXMLDO
function RemoveAttributesDOM(oNode,
if (oNode.nodeType != 1) return;
for (var i=0;i<vtList.length;i++) {
oNode.removeAttribute(vtLi
}
}
//void RemoveElementDOM(IXMLDOMEl
function RemoveElementDOM(oNode) {
var oParent = oNode.parentNode;
oParent.removeChild(oNode)
}
//IXMLDOMElement SetRootElementDOM(DOMDocum
function SetRootElementDOM(oDOM,szN
szEncoding = "string" == typeof(szEncoding) && szEncoding.length != 0 ? " encoding='" + szEncoding
+ "'" : "";
StripRootElementsDOM(oDOM)
oDOM.appendChild(oDOM.crea
oDOM.appendChild(oDOM.crea
return oDOM.documentElement;
}
//void StripRootElementsDOM(DOMDo
function StripRootElementsDOM(oDOM)
for (var i=0; i<oDOM.childNodes.length;i
if (oDOM.childNodes.item(i).n
oDOM.removeChild(oDOM.chil
}
}
if (oDOM.documentElement) oDOM.removeChild(oDOM.docu
}
window.onload = init;
function init() {
//removal of element
var oSection = [];
oSection[0] = oXML.selectSingleNode("//s
if (oSection[0]) RemoveElementDOM(oSection[
//adding a new element with attributes to food section
oSection[1] = oXML.selectSingleNode("//s
if (oSection[1]) {
AddAttributesDOM(AddElemen
AddAttributesDOM(AddElemen
//get reference to new element created
var oFood = AddElementDOM(oSection[1],
//add attributes to food
AddAttributesDOM(oFood,["n
//add elements and attributes to food
AddAttributesDOM(AddElemen
AddAttributesDOM(AddElemen
AddAttributesDOM(AddElemen
}
//change attributes
AddAttributesDOM(oXML.docu
RemoveAttributesDOM(oXML.d
DisplayXML();
}
function getISODateTime(datItem) {
var szDate = datItem.getYear() + "-";
szDate += (datItem.getMonth() < 9 ? "0" : "") + (datItem.getMonth() + 1) + "-";
szDate += (datItem.getDate() < 10 ? "0" : "") + datItem.getDate() + "T";
szDate += (datItem.getHours() < 10 ? "0" : "") + datItem.getHours() + ":";
szDate += (datItem.getMinutes() < 10 ? "0" : "") + datItem.getMinutes() + ":";
szDate += (datItem.getSeconds() < 10 ? "0" : "") + datItem.getSeconds();
return szDate;
}
function DisplayXML() {
if (oXSLT.readyState != 4) {
window.setTimeout("Display
} else {
if (wXML != null && "object" == typeof(wXML.opener) && wXML.opener != null) wXML.close();
wXML = window.open("about:blank",
wXML.document.write(oXML.t
wXML.document.close();
}
}
</script>
</head>
<body></body>
</html>
STEP 3
======
Open data.html in your browser
======
Open data.html in your browser
ASKER
I did that it display data.xml file
what I need is how to call each function in for example a command button.
For example :
function AddElementDOM(oParent,szNa me,szText)
what is the value of oParent,szName,szText to add new element.
Thanks
Mo
what I need is how to call each function in for example a command button.
For example :
function AddElementDOM(oParent,szNa
what is the value of oParent,szName,szText to add new element.
Thanks
Mo
ASKER
Sorry Brandon I didn’t see that the htm page start with the function init().
I have another question why the new element is not saved on data.xml?
Thanks
Mo
I have another question why the new element is not saved on data.xml?
Thanks
Mo
Because the XML Tree that is manipulated is stored in-memory on the client's memory namespace. If you want to save the results, you must change the following.
1. Rename data.html to data.hta
2. Change the following in DisplayXML()
function DisplayXML() {
if (oXSLT.readyState != 4) {
window.setTimeout("Display XML()",100 );
} else {
if (wXML != null && "object" == typeof(wXML.opener) && wXML.opener != null) wXML.close();
wXML = window.open("about:blank", "myXMLData ");
wXML.document.write(oXML.t ransformNo de(oXSLT)) ;
wXML.document.close();
oXML.save("data.xml");
}
}
If you want to have the changes reflected on the server, you must post the new XML Tree via XMLHTTP back to the server (IIS).
1. Rename data.html to data.hta
2. Change the following in DisplayXML()
function DisplayXML() {
if (oXSLT.readyState != 4) {
window.setTimeout("Display
} else {
if (wXML != null && "object" == typeof(wXML.opener) && wXML.opener != null) wXML.close();
wXML = window.open("about:blank",
wXML.document.write(oXML.t
wXML.document.close();
oXML.save("data.xml");
}
}
If you want to have the changes reflected on the server, you must post the new XML Tree via XMLHTTP back to the server (IIS).
ASKER
I got this message :
Permission Denied
Any suggestion?
Mo
Permission Denied
Any suggestion?
Mo
ASKER
I got this message :
Permission Denied
Any suggestion?
Mo
Permission Denied
Any suggestion?
Mo
ASKER
Thanks Brandon
It's working fine
last question how to post the new XML Tree via XMLHTTP
back to the server (IIS).
It's working fine
last question how to post the new XML Tree via XMLHTTP
back to the server (IIS).
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Errata
=======
postXML.asp
===========
<%
Dim oPayload
Set oPayload = Server.CreateObject("Micro soft.XMLDO M");
oPayload.async = False
oPayload.load Request
If oPayload.parseError.errorC ode = 0 Then
oPayload.save Server.MapPath("data.xml")
End If
%>
=======
postXML.asp
===========
<%
Dim oPayload
Set oPayload = Server.CreateObject("Micro
oPayload.async = False
oPayload.load Request
If oPayload.parseError.errorC
oPayload.save Server.MapPath("data.xml")
End If
%>
ASKER
Many Thanks,
PLEASE NOTE
-----------
1. In order to run the scripts successfully in ALL scenarios, please run them locally e.g. d:\data.html. This is because we are using the default Microsoft StyleSheet to show how the XML Data looks like.
<html>
<head>
<title>XMLDOM Manipulation</title>
<script language="javascript">
var oXML = new ActiveXObject("Microsoft.X
var oXSLT = new ActiveXObject("Microsoft.X
var wXML;
oXML.async = false;
oXSLT.async = true;
oXSLT.load("res://msxml.dl
/*****BEGINNING OF GENERIC SCRIPTS****/
/*****COPYRIGHT BY BRANDON DRIESEN****/
//IXMLDOMElement AddElementDOM(IXMLDOMEleme
function AddElementDOM(oParent,szNa
var oDOM = oParent.ownerDocument;
var newElement = oDOM.createElement(szName)
if ("string" == typeof(szText) && szText.length != 0) newElement.text = szText;
oParent.appendChild(newEle
return newElement;
}
//void AddAttributes(IXMLDOMEleme
function AddAttributesDOM(oNode,vtL
if (oNode.nodeType != 1) return;
for (var i=0;i<vtList.length;i+=2) {
oNode.setAttribute(vtList[
}
}
//void RemoveAttributesDOM(IXMLDO
function RemoveAttributesDOM(oNode,
if (oNode.nodeType != 1) return;
for (var i=0;i<vtList.length;i++) {
oNode.removeAttribute(vtLi
}
}
//void RemoveElementDOM(IXMLDOMEl
function RemoveElementDOM(oNode) {
var oParent = oNode.parentNode;
oParent.removeChild(oNode)
}
//IXMLDOMElement SetRootElementDOM(DOMDocum
function SetRootElementDOM(oDOM,szN
szEncoding = "string" == typeof(szEncoding) && szEncoding.length != 0 ? " encoding='" + szEncoding + "'" : "";
StripRootElementsDOM(oDOM)
oDOM.appendChild(oDOM.crea
oDOM.appendChild(oDOM.crea
return oDOM.documentElement;
}
//void StripRootElementsDOM(DOMDo
function StripRootElementsDOM(oDOM)
for (var i=0; i<oDOM.childNodes.length;i
if (oDOM.childNodes.item(i).n
oDOM.removeChild(oDOM.chil
}
}
if (oDOM.documentElement) oDOM.removeChild(oDOM.docu
}
window.onload = init;
function init() {
var root = SetRootElementDOM(oXML,"pa
AddAttributesDOM(root,["ti
var section = [];
section[0] = AddElementDOM(root,"sectio
AddAttributesDOM(section[0
section[1] = AddElementDOM(root,"sectio
AddAttributesDOM(section[1
AddAttributesDOM(AddElemen
AddAttributesDOM(AddElemen
DisplayXML();
}
function getISODateTime(datItem) {
var szDate = datItem.getYear() + "-";
szDate += (datItem.getMonth() < 9 ? "0" : "") + (datItem.getMonth() + 1) + "-";
szDate += (datItem.getDate() < 10 ? "0" : "") + datItem.getDate() + "T";
szDate += (datItem.getHours() < 10 ? "0" : "") + datItem.getHours() + ":";
szDate += (datItem.getMinutes() < 10 ? "0" : "") + datItem.getMinutes() + ":";
szDate += (datItem.getSeconds() < 10 ? "0" : "") + datItem.getSeconds();
return szDate;
}
function DisplayXML() {
if (oXSLT.readyState != 4) {
window.setTimeout("Display
} else {
if (wXML != null && "object" == typeof(wXML.opener) && wXML.opener != null) wXML.close();
wXML = window.open("about:blank",
wXML.document.write(oXML.t
wXML.document.close();
}
}
</script>
</head>
<body></body>
</html>