代码如下:
function main()
{
var dom = MakeDOM(null); try {
// Create a processing instruction targeted for xml.
var node = dom.createProcessingInstruction("xml",
"version='1.0'");
dom.appendChild(node);
node = null; // Create a processing instruction targeted for xml-stylesheet.
node = dom.createProcessingInstruction(
"xml-stylesheet",
"type='text/xml' href='test.xsl'");
dom.appendChild(node);
node = null; // Create a comment for the document.
node = dom.createComment(
"sample xml file created using XML DOM object.");
dom.appendChild(node);
node=null; // Create the root element.
var root = dom.createElement("root"); // Create a "created" attribute for the root element and
// assign the "using dom" character data as the attribute value.
var attr = dom.createAttribute("created");
attr.value = "using dom";
root.setAttributeNode(attr);
attr = null; // Add the root element to the DOM instance.
dom.appendChild(root); // Insert a newline + tab.
root.appendChild(dom.createTextNode("\n\t"));
// Create more nodes and add them to the root element just created.
// Add a text node as <node1>.
node = dom.createElement("node1");
node.text = "some character data";
root.appendChild(node);
// Add a newline + tab.
root.appendChild(dom.createTextNode("\n\t")); // Add a CDATA section as <node2>.
node=null;
node = dom.createElement("node2");
var cd =dom.createCDATASection("some -up text");
node.appendChild(cd);
cd = null;
root.appendChild(node); // Create an element (<node3>) to hold three empty subelements.
node = null;
node = dom.createElement("node3");
// Create a document fragment to be added to <node3>.
var frag = dom.createDocumentFragment();
// Add a newline + tab + tab as a text node and an empty subnode.
frag.appendChild(dom.createTextNode("\n\t\t"));
frag.appendChild(dom.createElement("subNode1")); // Add a newline + tab + tab as a text node and an empty subnode.
frag.appendChild(dom.createTextNode("\n\t\t"));
frag.appendChild(dom.createElement("subNode2")); // Add a newline + tab + tab as a text node and an empty subnode.
frag.appendChild(dom.createTextNode("\n\t\t"));
frag.appendChild(dom.createElement("subNode3")); // Add a newline + tab.
frag.appendChild(dom.createTextNode("\n\t"));
node.appendChild(frag);
root.appendChild(node);
frag=null;
node=null;
// Add a newline.
root.appendChild(dom.createTextNode("\n")); // Save the XML document to a file.
dom.save("dynamDom.xml"); root = null;
dom = null;
}
catch (e)
{
alert(e.description);
}}function MakeDOM(progID)
{
if (progID == null) {
progID = "msxml2.DOMDocument.3.0";
} var dom;
try {
dom = new ActiveXObject(progID);
dom.async = false;
dom.validateOnParse = false;
dom.resolveExternals = false;
}
catch (e) {
alert(e.description);
}
return dom;
}
function main()
{
var dom = MakeDOM(null); try {
// Create a processing instruction targeted for xml.
var node = dom.createProcessingInstruction("xml",
"version='1.0'");
dom.appendChild(node);
node = null; // Create a processing instruction targeted for xml-stylesheet.
node = dom.createProcessingInstruction(
"xml-stylesheet",
"type='text/xml' href='test.xsl'");
dom.appendChild(node);
node = null; // Create a comment for the document.
node = dom.createComment(
"sample xml file created using XML DOM object.");
dom.appendChild(node);
node=null; // Create the root element.
var root = dom.createElement("root"); // Create a "created" attribute for the root element and
// assign the "using dom" character data as the attribute value.
var attr = dom.createAttribute("created");
attr.value = "using dom";
root.setAttributeNode(attr);
attr = null; // Add the root element to the DOM instance.
dom.appendChild(root); // Insert a newline + tab.
root.appendChild(dom.createTextNode("\n\t"));
// Create more nodes and add them to the root element just created.
// Add a text node as <node1>.
node = dom.createElement("node1");
node.text = "some character data";
root.appendChild(node);
// Add a newline + tab.
root.appendChild(dom.createTextNode("\n\t")); // Add a CDATA section as <node2>.
node=null;
node = dom.createElement("node2");
var cd =dom.createCDATASection("some -up text");
node.appendChild(cd);
cd = null;
root.appendChild(node); // Create an element (<node3>) to hold three empty subelements.
node = null;
node = dom.createElement("node3");
// Create a document fragment to be added to <node3>.
var frag = dom.createDocumentFragment();
// Add a newline + tab + tab as a text node and an empty subnode.
frag.appendChild(dom.createTextNode("\n\t\t"));
frag.appendChild(dom.createElement("subNode1")); // Add a newline + tab + tab as a text node and an empty subnode.
frag.appendChild(dom.createTextNode("\n\t\t"));
frag.appendChild(dom.createElement("subNode2")); // Add a newline + tab + tab as a text node and an empty subnode.
frag.appendChild(dom.createTextNode("\n\t\t"));
frag.appendChild(dom.createElement("subNode3")); // Add a newline + tab.
frag.appendChild(dom.createTextNode("\n\t"));
node.appendChild(frag);
root.appendChild(node);
frag=null;
node=null;
// Add a newline.
root.appendChild(dom.createTextNode("\n")); // Save the XML document to a file.
dom.save("dynamDom.xml"); root = null;
dom = null;
}
catch (e)
{
alert(e.description);
}}function MakeDOM(progID)
{
if (progID == null) {
progID = "msxml2.DOMDocument.3.0";
} var dom;
try {
dom = new ActiveXObject(progID);
dom.async = false;
dom.validateOnParse = false;
dom.resolveExternals = false;
}
catch (e) {
alert(e.description);
}
return dom;
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货