{"id":64,"date":"2008-04-18T11:47:45","date_gmt":"2008-04-18T09:47:45","guid":{"rendered":"http:\/\/www.crydust.be\/blog\/?p=64"},"modified":"2008-04-18T11:48:32","modified_gmt":"2008-04-18T09:48:32","slug":"parsing-plist-files-in-asp-with-microsoftxmldom","status":"publish","type":"post","link":"https:\/\/www.crydust.be\/blog\/2008\/04\/18\/parsing-plist-files-in-asp-with-microsoftxmldom\/","title":{"rendered":"Parsing plist files in asp with Microsoft.XMLDOM"},"content":{"rendered":"<p>Plist files are more and more ubiquitous. Apple uses them for its configuration files and we use them to describe exercise content. They aren&#8217;t very pleasant to read for humans, but that doesn&#8217;t mean computers dislike reading them. So I wrote a little javascript to parse a plist file to an object. Hope you like it.<\/p>\n<p><!--more--><\/p>\n<pre class=\"prettyprint\">&lt;script language=&quot;javascript&quot; runat=&quot;server&quot;&gt;\r\nXMLUtil = function() {\r\n  \/\/ nothing\r\n}\r\n\r\nXMLUtil.createXmlDom = function(xmlFile, xslFile){\r\n  var result = null;\r\n  try{\r\n    result = new ActiveXObject(&quot;Msxml2.DOMDocument.6.0&quot;);\r\n    result.validateOnParse = false;\r\n    result.setProperty(&quot;AllowDocumentFunction&quot;, true);\r\n    result.setProperty(&quot;AllowXsltScript&quot;, true);\r\n    result.setProperty(&quot;ProhibitDTD&quot;, false);\r\n  }catch(e){\r\n    try{\r\n      result = new ActiveXObject(&quot;MSXML2.DOMDocument.3.0&quot;);\r\n    }catch(e){\r\n      try{\r\n        result = new ActiveXObject(&quot;Microsoft.XMLDOM&quot;);\r\n      }catch(e){\r\n        Response.Write(&quot;ERROR MSXML is not installed.&quot;);\r\n        result = null;\r\n      }\r\n    }\r\n  }\r\n  if(typeof result == &quot;object&quot;){\r\n    \/\/ &quot;XSLPattern&quot; in 3.0. &quot;XPath&quot; in 4.0, 5.0, 6.0\r\n    result.setProperty(&quot;SelectionLanguage&quot;, &quot;XPath&quot;);\r\n    result.async = false;\r\n    if(typeof xmlFile == &quot;string&quot;){\r\n      if(xmlFile.indexOf(&quot;:&quot;) == -1){\r\n        xmlFile = Server.MapPath(xmlFile);\r\n      }\r\n      result.load(xmlFile);\r\n    }\r\n    if (result.parseError.errorCode != 0) {\r\n      Response.Write(&quot;ERROR &quot; + result.parseError.reason);\r\n      result = null;\r\n    }else{\r\n      if(typeof xslFile != &quot;undefined&quot;){\r\n        if(typeof xslFile == &quot;string&quot;){\r\n          xslFile = XMLUtil.createXmlDom(xslFile);\r\n        }\r\n        if(xslFile.parseError.errorCode != 0){\r\n          Response.Write(&quot;ERROR &quot; + xslFile.parseError.reason);\r\n          result = null;\r\n        }else{\r\n          var transformedFile = XMLUtil.createXmlDom();\r\n          result.transformNodeToObject(xslFile, transformedFile);\r\n          result = transformedFile;\r\n        }\r\n      }\r\n    }\r\n  }\r\n  return result;\r\n}\r\n\r\nXMLUtil.parsePlistDict = function(dictNode){\r\n  var result = new Object();\r\n  var keyNodes = dictNode.selectNodes(&quot;key&quot;);\r\n  for(var i = 0, n = keyNodes.length; i &lt; n; i++){\r\n    var key = keyNodes[i].firstChild.nodeValue;\r\n    var val = XMLUtil.parsePlistValue(keyNodes[i].nextSibling);\r\n    result[key] = val;\r\n  }\r\n  return result;\r\n}\r\n\r\nXMLUtil.parsePlistValue = function(valueNode){\r\n  var result = null;\r\n  switch(valueNode.nodeName){\r\n    case &quot;true&quot;:\r\n      result = true;\r\n      break;\r\n    case &quot;false&quot;:\r\n      result = false;\r\n      break;\r\n    case &quot;string&quot;:\r\n      if(valueNode.firstChild != undefined){\r\n        result = valueNode.firstChild.nodeValue;\r\n        \/\/ result = unescape(result);\r\n        \/\/ result = result.replace(\/\\+\/g, &quot; &quot;);\r\n      }\r\n      break;\r\n    case &quot;dict&quot;:\r\n      result = XMLUtil.parsePlistDict(valueNode);\r\n      break;\r\n    case &quot;array&quot;:\r\n      result = [];\r\n      for(var i = 0, n = valueNode.childNodes.length; i &lt; n; i++){\r\n        result.push(XMLUtil.parsePlistValue(valueNode.childNodes[i]));\r\n      }\r\n      break;\r\n    default:\r\n      Response.Write(&quot;ERROR can't parse &quot;+valueNode.nodeName);\r\n  }\r\n  return result;\r\n}\r\n\r\n\/\/ usage:\r\nvar plistXml = XMLUtil.createXmlDom(&quot;c:\\\\example.plist&quot;);\r\nvar obj = XMLUtil.parsePlistDict(plistXml.firstChild.firstChild);\r\n&lt;\/script&gt;<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Plist files are more and more ubiquitous. Apple uses them for its configuration files and we use them to describe exercise content. They aren&#8217;t very pleasant to read for humans, but that doesn&#8217;t mean computers dislike reading them. So I &hellip; <a href=\"https:\/\/www.crydust.be\/blog\/2008\/04\/18\/parsing-plist-files-in-asp-with-microsoftxmldom\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-64","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/www.crydust.be\/blog\/wp-json\/wp\/v2\/posts\/64","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.crydust.be\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.crydust.be\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.crydust.be\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.crydust.be\/blog\/wp-json\/wp\/v2\/comments?post=64"}],"version-history":[{"count":0,"href":"https:\/\/www.crydust.be\/blog\/wp-json\/wp\/v2\/posts\/64\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.crydust.be\/blog\/wp-json\/wp\/v2\/media?parent=64"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.crydust.be\/blog\/wp-json\/wp\/v2\/categories?post=64"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.crydust.be\/blog\/wp-json\/wp\/v2\/tags?post=64"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}