Skip to content

Instantly share code, notes, and snippets.

@GoodChancer
Created March 4, 2017 08:27
Show Gist options
  • Save GoodChancer/94e37fafeb34ac6eaad273447f861c53 to your computer and use it in GitHub Desktop.
Save GoodChancer/94e37fafeb34ac6eaad273447f861c53 to your computer and use it in GitHub Desktop.
XML Generator
<?php
$json = include('getdata.php');
$i=1;
/* create a dom document with encoding utf8 */
//create a new xmlwriter object
$xml = new XMLWriter();
//using memory for string output
$xml->openMemory();
//set the indentation to true (if false all the xml will be written on one line)
$xml->setIndent(true);
$xml->setIndentString("\t");
//create the document tag, you can specify the version and encoding here
$xml->startDocument('1.0', 'UTF-8');
$xml->writeDTD("plist","-//Apple Computer//DTD PLIST 1.0//EN","http://www.apple.com/DTDs/PropertyList-1.0.dtd");
//Create an element
$xml->startElement("plist");
$xml->writeAttribute('version','1.0');
$xml->startElement("dict");
$xml->writeElement("key", "Major Version");
$xml->writeElement("integer", "1");
$xml->writeElement("key", "Minor Version");
$xml->writeElement("integer", "1");
$xml->writeElement("key", "Application Version");
$xml->writeElement("string", "12.5.5.5");
$xml->writeElement("key", "Features");
$xml->writeElement("integer", "5");
$xml->writeElement("key", "Tracks");
$xml->startElement("dict");
//loop me
while($json['Albums']) {
$album = array_shift($json['Albums']);
foreach($album['Tracks'] as $title) {
$album1 = $album['Album'];
$artist1 = $album['Artist'];
$title1 = $title['Title'];
$xml->writeElement("key", "{$i}");
$xml->startElement("dict");
$xml->writeElement("key", "Track ID");
$xml->writeElement("integer", "{$i}");
$xml->writeElement("key", "Name");
$xml->writeElement("string", "{$title1}");
$xml->writeElement("key", "Artist");
$xml->writeElement("string", "{$artist1}");
$xml->writeElement("key", "Album");
$xml->writeElement("string", "{$album1}");
$xml->writeElement("key", "Track Type");
$xml->writeElement("string", "Remote");
$xml->writeElement("key", "Apple Music");
$xml->writeElement("true");
$xml->endElement();
$i++;
}
}
$xml->endElement();
$xml->writeElement("key", "Playlists");
$xml->startElement("array");
$xml->startElement("dict");
$xml->writeElement("key", "Name");
$xml->writeElement("string", "Best of 2016 Playlist");
$xml->writeElement("key", "Description");
$xml->writeElement("string", "");
$xml->writeElement("key", "All Items");
$xml->writeElement("true");
$xml->writeElement("key", "Playlist Items");
$xml->startElement("array");
//loop me
while($json['Albums']) {
$album = array_shift($json['Albums']);
foreach($album['Tracks'] as $title) {
$xml->startElement("dict");
$xml->writeElement("key", "Track ID");
$xml->writeElement("integer", "{$i}");
$xml->endElement();
$i++;
}
}
$xml->endElement();
$xml->endElement();
$xml->endElement();
$xml->endElement();
$xml->endElement();
/* save xml file */
file_put_contents('xml/test-playlist.xml', $xml->outputMemory());
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment