Some Sample Code to illustrate XML manipulation by JScript
It seems that some folks just want free code.
Question:
Hi, I am not sure if it's really that on 18 Mar 2004 wrote in the newsgroups how to do stuff with XML files in WSH.
"I do this all the time and have tons of script code that read/manipulate XML files."
I wonder if you could send me one or two jscript files of yours that read XML files maybe put stuff into an array and sort it.
The easier and smaller the codesamples the better.
You'd really help me out a lot.
Answer:
First, you must become familiar with the MS XML Documentation . Asking for code samples is not a long-term solution.
So, I will only do part of what you ask - you need to be able to finish it up yourself by finding and reading necessary documentation.
The following code illustrates how to read and select XML nodes based on various criteria as well as a simple function that enumerates through the list of selected nodes and put them into an Array.
//David
var objArray;
var i;
var objXML = new ActiveXObject( "Microsoft.XMLDOM" );
objXML.loadXML( "<root>\r\n" +
"<element1 attribute1='value1' attribute2='value2' />\r\n" +
"<element1 attribute1='value3' />\r\n" +
"<element2 attribute2='value4' />\r\n" +
"</root>\r\n" );
var root = objXML.documentElement;
WScript.Echo( "Test XML:\r\n" + root.xml );
//
// Just select element1 and not element2
//
WScript.Echo( "\r\nJust element1:" );
objArray = NodesToArray( root.selectNodes( "element1" ) );
for ( i = 0; i < objArray.length; i++ )
{
WScript.Echo( objArray[ i ].xml );
}
//
// Just select element1 with attribute named "attribute2"
//
WScript.Echo( "\r\nJust element1 with attribute2:" );
objArray = NodesToArray( root.selectNodes( "element1[@attribute2]" ) );
for ( i = 0; i < objArray.length; i++ )
{
WScript.Echo( objArray[ i ].xml );
}
function NodesToArray(
objNodes
)
{
var objArray = new Array();
var objNode;
while ( ( objNode = objNodes.nextNode() ) != null )
{
objArray[ objArray.length ] = objNode;
}
return objArray;
}