-
-
Save koyoyo/5149024 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function generate_xml_element( $dom, $data ) { | |
if ( empty( $data['name'] ) ) | |
return false; | |
// Create the element | |
$element_value = ( ! empty( $data['value'] ) ) ? $data['value'] : null; | |
$element = $dom->createElement( $data['name'], $element_value ); | |
// Add any attributes | |
if ( ! empty( $data['attributes'] ) && is_array( $data['attributes'] ) ) { | |
foreach ( $data['attributes'] as $attribute_key => $attribute_value ) { | |
$element->setAttribute( $attribute_key, $attribute_value ); | |
} | |
} | |
// Any other items in the data array should be child elements | |
foreach ( $data as $data_key => $child_data ) { | |
if ( ! is_numeric( $data_key ) ) | |
continue; | |
$child = generate_xml_element( $dom, $child_data ); | |
if ( $child ) | |
$element->appendChild( $child ); | |
} | |
return $element; | |
} | |
function generate_author( $author, $primary_contact ){ | |
if($primary_contact == 0){ | |
$primary_contact_boolean = true; | |
} | |
else { | |
$primary_contact_boolean = false; | |
} | |
return array( | |
'name' => 'author', | |
'attributes' => array( | |
'primary_contact' => $primary_contact_boolean, | |
), | |
array( | |
'name' => 'firstname', | |
'value' => $author['firstname'], | |
), | |
array( | |
'name' => 'lastname', | |
'value' => $author['lastname'], | |
), | |
array( | |
'name' => 'affiliation', | |
'value' => $author['affiliation'], | |
), | |
array( | |
'name' => 'country', | |
'value' => $author['country'], | |
), | |
array( | |
'name' => 'email', | |
'value' => $author['email'], | |
), | |
) | |
} | |
function generate_article( $article ){ | |
return array( | |
'name' => 'article', | |
array( | |
'name' => 'title', | |
'attributes' => array( | |
'locale' => 'en_US', | |
), | |
'value' => $article['title'], | |
), | |
array( | |
'name' => 'abstract', | |
'attributes' => array( | |
'locale' => 'en_US', | |
), | |
'value' => $article['abstract'], | |
), | |
for($i = 0; $i < count($article['author']); $i++){ | |
generate_article($article['author'][$i], $i), | |
} | |
array( | |
'name' => 'pages', | |
'value' => $article['page'], | |
), | |
array( | |
'name' => 'date_published', | |
'value' => $article['date_published'], | |
), | |
array( | |
'name' => 'galley', | |
'attributes' => array( | |
'locale' => 'en_US', | |
), | |
array( | |
'name' => 'label', | |
'value' => $article['label'], | |
), | |
array( | |
'name' => 'file', | |
array( | |
'name' => 'href', | |
'attributes' => array( | |
'mime_type' => $article['mime_type'], | |
'src' => $article['src'], | |
), | |
), | |
), | |
), | |
} | |
function generate_section( $section ){ | |
return array( | |
'name' => 'section', | |
array( | |
'name' => 'title', | |
'attributes' => array( | |
'locale' => 'en_US' | |
), | |
'value' => $section['title'] | |
), | |
array( | |
'name' => 'abbrev', | |
'attributes' => array( | |
'locale' => 'en_US' | |
), | |
'value' => $section['abbrev'] | |
), | |
for( $i = 0; $i < count($section['article']); $i++ ){ | |
generate_article( $section['article'][$i] ); | |
} | |
), | |
} | |
$data = array( | |
'name' => 'issue', // "name" required, all else optional | |
'attributes' => array( | |
'published' => 'true', | |
'current' => 'true', | |
'public_id' => 'XX-XXXXX', | |
), | |
array( | |
'name' => 'title', | |
'attributes' => array( | |
'locale' => 'en_US', | |
), | |
'value'=> 'TITLE', | |
), | |
array( | |
'name' => 'volume', | |
'value'=> 'INT VALUE', | |
), | |
array( | |
'name' => 'number', | |
'value'=> 'INT NUMBER', | |
), | |
array( | |
'name' => 'year', | |
'value'=> 'INT YEAR', | |
), | |
array( | |
'name' => 'cover', | |
'attributes' => array( | |
'locale' => 'en_US', | |
), | |
array( | |
'name' => 'caption', | |
'value' => 'CAPTION VALUE' | |
) | |
), | |
array( | |
'name' => 'date_published', | |
'value'=> '2010-04-01', | |
), | |
array( | |
'name' => 'access_date', | |
'value'=> '2010-03-01', | |
), | |
array( | |
'name' => 'section', | |
array( | |
'name' => 'title', | |
'attributes' => array( | |
'locale' => 'en_US' | |
), | |
'value' => 'VALUE' | |
), | |
array( | |
'name' => 'abbrev', | |
'attributes' => array( | |
'locale' => 'en_US' | |
), | |
'value' => 'VALUE' | |
), | |
array( | |
'name' => 'article', | |
array( | |
'name' => 'title', | |
'attributes' => array( | |
'locale' => 'en_US', | |
), | |
'value' => 'ARTICLE VALUE' | |
), | |
array( | |
'name' => 'abstract', | |
'attributes' => array( | |
'locale' => 'en_US', | |
), | |
'value' => 'ABSTRACT VALUE', | |
), | |
array( | |
'name' => 'author', | |
'attributes' => array( | |
'primary_contact' => true, | |
), | |
array( | |
'name' => 'firstname', | |
'value' => 'FIRST NAME' | |
), | |
array( | |
'name' => 'lastname', | |
'value' => 'LAST NAME' | |
), | |
array( | |
'name' => 'affiliation', | |
'value' => 'Affiliation value' | |
), | |
array( | |
'name' => 'country', | |
'value' => 'TH' | |
), | |
array( | |
'name' => 'email', | |
'value' => 'Email' | |
), | |
), | |
array( | |
'name' => 'pages', | |
'value' => '155-200' | |
), | |
array( | |
'name' => 'date_published', | |
'value' => '2010-04-01' | |
), | |
array( | |
'name' => 'galley', | |
'attributes' => array( | |
'locale' => 'en_US', | |
), | |
array( | |
'name' => 'label', | |
'value' => 'LABEL VAL' | |
), | |
array( | |
'name' => 'file', | |
array( | |
'name' => 'href', | |
'attributes' => array( | |
'mime_type' => 'application/pdf', | |
'src' => 'http://XXXXXX', | |
) | |
) | |
), | |
), | |
) | |
), | |
); | |
print_r($data); | |
$doc = new DOMDocument(); | |
$child = generate_xml_element( $doc, $data ); | |
if ( $child ) | |
$doc->appendChild( $child ); | |
$doc->formatOutput = true; // Add whitespace to make easier to read XML | |
$xml = $doc->saveXML(); | |
print $xml; | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment