Parsing plist files in asp with Microsoft.XMLDOM

Plist files are more and more ubiquitous. Apple uses them for its configuration files and we use them to describe exercise content. They aren’t very pleasant to read for humans, but that doesn’t mean computers dislike reading them. So I wrote a little javascript to parse a plist file to an object. Hope you like it.

<script language="javascript" runat="server">
XMLUtil = function() {
  // nothing
}

XMLUtil.createXmlDom = function(xmlFile, xslFile){
  var result = null;
  try{
    result = new ActiveXObject("Msxml2.DOMDocument.6.0");
    result.validateOnParse = false;
    result.setProperty("AllowDocumentFunction", true);
    result.setProperty("AllowXsltScript", true);
    result.setProperty("ProhibitDTD", false);
  }catch(e){
    try{
      result = new ActiveXObject("MSXML2.DOMDocument.3.0");
    }catch(e){
      try{
        result = new ActiveXObject("Microsoft.XMLDOM");
      }catch(e){
        Response.Write("ERROR MSXML is not installed.");
        result = null;
      }
    }
  }
  if(typeof result == "object"){
    // "XSLPattern" in 3.0. "XPath" in 4.0, 5.0, 6.0
    result.setProperty("SelectionLanguage", "XPath");
    result.async = false;
    if(typeof xmlFile == "string"){
      if(xmlFile.indexOf(":") == -1){
        xmlFile = Server.MapPath(xmlFile);
      }
      result.load(xmlFile);
    }
    if (result.parseError.errorCode != 0) {
      Response.Write("ERROR " + result.parseError.reason);
      result = null;
    }else{
      if(typeof xslFile != "undefined"){
        if(typeof xslFile == "string"){
          xslFile = XMLUtil.createXmlDom(xslFile);
        }
        if(xslFile.parseError.errorCode != 0){
          Response.Write("ERROR " + xslFile.parseError.reason);
          result = null;
        }else{
          var transformedFile = XMLUtil.createXmlDom();
          result.transformNodeToObject(xslFile, transformedFile);
          result = transformedFile;
        }
      }
    }
  }
  return result;
}

XMLUtil.parsePlistDict = function(dictNode){
  var result = new Object();
  var keyNodes = dictNode.selectNodes("key");
  for(var i = 0, n = keyNodes.length; i < n; i++){
    var key = keyNodes[i].firstChild.nodeValue;
    var val = XMLUtil.parsePlistValue(keyNodes[i].nextSibling);
    result[key] = val;
  }
  return result;
}

XMLUtil.parsePlistValue = function(valueNode){
  var result = null;
  switch(valueNode.nodeName){
    case "true":
      result = true;
      break;
    case "false":
      result = false;
      break;
    case "string":
      if(valueNode.firstChild != undefined){
        result = valueNode.firstChild.nodeValue;
        // result = unescape(result);
        // result = result.replace(/\+/g, " ");
      }
      break;
    case "dict":
      result = XMLUtil.parsePlistDict(valueNode);
      break;
    case "array":
      result = [];
      for(var i = 0, n = valueNode.childNodes.length; i < n; i++){
        result.push(XMLUtil.parsePlistValue(valueNode.childNodes[i]));
      }
      break;
    default:
      Response.Write("ERROR can't parse "+valueNode.nodeName);
  }
  return result;
}

// usage:
var plistXml = XMLUtil.createXmlDom("c:\\example.plist");
var obj = XMLUtil.parsePlistDict(plistXml.firstChild.firstChild);
</script>

Comments are closed.