Skip to content

Instantly share code, notes, and snippets.

@cKlee
Last active December 16, 2015 21:38
Show Gist options
  • Save cKlee/5500830 to your computer and use it in GitHub Desktop.
Save cKlee/5500830 to your computer and use it in GitHub Desktop.
Given a PicaPlus XML DomDocument this function parses the DomDocument into an php array
<?php
/**
* parse an picaplusxml record into an array
* @param object $dom: domcument
* @return array picaplusArray
*/
function picaplusxml2Array(DomDocument $dom){
$numberOfRecords = $dom->getElementsByTagNameNS('http://www.loc.gov/zing/srw/', 'numberOfRecords');
$this->numberOfRecords = $numberOfRecords->item(0)->nodeValue;
$picaplusArray = array();
$parentName = '';
$records = $dom->getElementsByTagNameNS('http://www.oclcpica.org/xmlns/ppxml-1.0', 'record');
$x = 0;
foreach($records as $record) {
$i = 0;
foreach ($record->getElementsByTagNameNS('http://www.oclcpica.org/xmlns/ppxml-1.0', 'tag') as $element) {
if($element->hasAttribute('id')){
// field / tag
$parentName = $element->getAttributeNode('id')->nodeValue;
$picaplusArray[$x][$parentName][] = array();
foreach($element->getElementsByTagNameNS('http://www.oclcpica.org/xmlns/ppxml-1.0', 'subf') as $child){
$elementArray = array();
$nodeId = $child->getAttributeNode('id')->nodeValue;
$elementArray[$nodeId] = $child->nodeValue;
$count = count($picaArray[$x][$parentName])-1;
array_push($picaplusArray[$x][$parentName][$count],$elementArray);
}
}
$i++;
}
$x++;
}
//print_r($picaplusArray);
return $picaplusArray;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment