Export (0) Print
Expand All

JScript Code (next.js)

[This sample code uses features that were first implemented in MSXML 5.0 for Microsoft Office Applications.]

var xs, xd;

main();

function main() 
{
  try {
    xd = new ActiveXObject("MSXML2.DOMDocument.6.0");
  }
  catch (e) {
    WScript.Echo("Microsoft XML Core Services (MSXML) 6.0 is not installed.\n"
          +"Download and install MSXML 6.0 from http://msdn.microsoft.com/xml\n"
          +"before continuing.");
    return;
  }

  try {
    xd.async = false;
    xd.validateOnParse = false;
    xd.setProperty("ResolveExternals", true);
    xd.load("books.xml");
    xd.setProperty("MultipleErrorMessages", true);
  }
  catch (e) {
    WScript.Echo("can't load books.xml : " + e.description);
    return;
  }

  // Validate the XML document, relying on the xsi:schemaLocation 
  // attribute of the document element to resolve where to read the 
  // schema definition.
  var err = xd.validate();
  if (err.errorCode != 0 ) { 
     var allErrs = err.allErrors;
     var str="";
     i = 0
     while (null != (e=allErrs.next)) {
        str += ("errorItem["+ i++ +"]: "+e.reason+"\n"); 
     } 
     WScript.Echo(str);
  }
  else
    WScript.Echo("valid dom as follows:\n" + xd.xml);

}


Try It!

  1. Copy the XML data (books.xml), and paste it into a text file. Save the file as books.xml.

  2. Copy the XSD listing (books.xsd), and paste it into a text file. Save the file as books.xsd, in the same directory where you saved books.xml.

  3. Copy the JScript listing above, and paste it into a text file. Save the file as next.js, in the same directory where you saved books.xml and books.xsd.

  4. Double click the next.js file from Windows Explorer to launch the application. Alternatively, you can type "next.js" from a command prompt.

    Note Note

    Under operating systems other than Windows 2000 or Windows XP, you might need to install Windows Scripting Host (wscript.exe), if it is not already installed.

  5. Verify that your output is the same as that listed in Output for the next Example.

Show:
© 2014 Microsoft