Skip to content

Instantly share code, notes, and snippets.

@iamamused
Created July 24, 2012 15:47
Show Gist options
  • Save iamamused/3170794 to your computer and use it in GitHub Desktop.
Save iamamused/3170794 to your computer and use it in GitHub Desktop.
PList to JSON Converter
<?php
function plist2json($child) {
switch($child->nodeName) {
case 'dict':
$d = new StdClass();
$nodes = $child->childNodes;
for($i = 0; $i < $nodes->length; $i++){
if ($nodes->item($i)->nodeName == 'key'){
$key = $nodes->item($i)->textContent;
$i++;
while ($nodes->item($i)->nodeName == "#text") {
$i++;
}
$d->$key = plist2json($nodes->item($i));
}
}
return $d;
break;
case 'array':
$a = array();
$nodes = $child->childNodes;
for($i = 0; $i < $nodes->length; $i++){
if ($nodes->item($i)->nodeName != "#text")
$a[] = plist2json($nodes->item($i));
}
return $a;
break;
case 'string':
return $child->textContent;
break;
case 'data':
return $child->textContent;
break;
case 'real':
return $child->textContent;
break;
case 'integer':
return $child->textContent;
break;
case 'true':
return true;
break;
case 'false':
return false;
break;
}
}
$xml = "PLIST-CONTENT";
$doc = new DOMDocument();
$doc->loadXML($xml);
foreach($doc->documentElement->childNodes as $child) {
if ($child->nodeName != "#text") {
$output = plist2json($child);
break;
}
}
header("Content-type: application/json");
echo json_encode($output);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment