Sabtu, 30 November 2024

Instantiate an XML DOM Object (JScript)

 

Instantiate an XML DOM Object (JScript)

This tutorial uses or creates the following files:

FileDescription

Source: InstantiateDOM.js

JScript source code.

Output

Output from the application.

To create the InstantiateDOM JScript project

  1. Create a JScript project. For detailed instructions on how to do this, see Set Up My JScript Project.

  2. Name the project folder InstantiateDOMProj.

Next, we'll add the source code for the InstantiateDOM project.


JScript Source File (InstantiateDOM.js)

J#
// Instantiate a DOM object at run time.
var dom = new ActiveXObject("msxml2.DOMDocument.6.0");
dom.async = false;
dom.resolveExternals = false;
dom.loadXML("<a>A</a>");

// Display the content of the object.
alert("dom: "+dom.xml);

// Helper function
function alert(str)
{
   WScript.Echo(str);
}

Try It!

  1. Copy the code listing above and paste it into your preferred JScript code editor.

  2. Save the file as with InstantiateDOM.js in the current project folder.

  3. Run the JScript file.

    You can run the script using the "cscript InstantiateDOM.js" command in aComand Promnpt window for console script execution or by double-clicking on InstantiateDOM.js in Windows Explorer to run it as a Windows script using wscript.exe.

    The result should be the output shown in the following topic.

Output from the InstantiateDOM.js Example

When you run the InstantiateDOM project, you should get the following output:

dom: <a>A</a>

The next tutorial shows how to load an XML file into an XML DOM object

Tidak ada komentar: