Share via


Jscript Code (inline.js)

 

This topic provides JScript example code.

Try It!

  1. Copy the first XML/XSD file from the resource files, and paste it into a text file. Save the file as valid.xml.

  2. Copy the second XML/XSD file from the resource files, and paste it into a text file. Save the file as notValid.xml, in the same directory where you saved valid.xml.

  3. Copy the JScript listing above, and paste it into a text file. Save the file as inline.js, in the same directory you used in previous steps.

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

    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 the output for this example.

inline.js

var sOutput = validateFile("valid.xml");
sOutput = sOutput + validateFile("notValid.xml");
WScript.Echo(sOutput);

function validateFile(strFile)
{
  // Create an XML DOMDocument object.
  var x = new ActiveXObject("MSXML2.DOMDocument.6.0");

  //Load and validate the specified file into the DOM.
  x.async = false;
  x.validateOnParse = true;
  x.resolveExternals = true;
  x.setProperty("UseInlineSchema", true);
  x.load(strFile);

  // Return validation results in message to the user.
  if (x.parseError.errorCode != 0)
  {
     return("Validation failed on " + strFile + 
            "\n=====================" +
            "\nReason: " + x.parseError.reason + 
            "\nSource: " + x.parseError.srcText + 
            "\nLine: " + x.parseError.line + "\n");
  }
  else 
     return("Validation succeeded for " + strFile + 
            "\n======================\n" +
            x.xml + "\n");
}