Advertisement
Viper007Bond

Array to XML

Jun 27th, 2011
344
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.88 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4.  
  5. <atom:entry xmlns:atom='http://www.w3.org/2005/Atom' xmlns:gd='http://schemas.google.com/g/2005'>
  6.     <atom:category scheme='http://schemas.google.com/g/2005#kind' term='http://schemas.google.com/contact/2008#contact' />
  7.     <gd:name>
  8.         <gd:givenName>Elizabeth</gd:givenName>
  9.         <gd:familyName>Bennet</gd:familyName>
  10.         <gd:fullName>Elizabeth Bennet</gd:fullName>
  11.     </gd:name>
  12.     <atom:content type='text'>Notes</atom:content>
  13.     <gd:email rel='http://schemas.google.com/g/2005#work' primary='true' address='liz@gmail.com' displayName='E. Bennet' />
  14.     <gd:email rel='http://schemas.google.com/g/2005#home' address='liz@example.org' />
  15.     <gd:phoneNumber rel='http://schemas.google.com/g/2005#work' primary='true'>
  16.         (206)555-1212
  17.     </gd:phoneNumber>
  18.     <gd:phoneNumber rel='http://schemas.google.com/g/2005#home'>
  19.         (206)555-1213
  20.     </gd:phoneNumber>
  21.     <gd:im address='liz@gmail.com' protocol='http://schemas.google.com/g/2005#GOOGLE_TALK' primary='true' rel='http://schemas.google.com/g/2005#home' />
  22.     <gd:structuredPostalAddress rel='http://schemas.google.com/g/2005#work' primary='true'>
  23.         <gd:city>Mountain View</gd:city>
  24.         <gd:street>1600 Amphitheatre Pkwy</gd:street>
  25.         <gd:region>CA</gd:region>
  26.         <gd:postcode>94043</gd:postcode>
  27.         <gd:country>United States</gd:country>
  28.         <gd:formattedAddress>
  29.             1600 Amphitheatre Pkwy Mountain View
  30.         </gd:formattedAddress>
  31.     </gd:structuredPostalAddress>
  32. </atom:entry>
  33.  
  34. */
  35.  
  36. $data = array(
  37.     '_name' => 'atom:entry', // "_name" needs to be the first item in each element array
  38.     '_attributes' => array(
  39.         'xmlns:atom' => 'http://www.w3.org/2005/Atom',
  40.         'xmlns:gd' => 'http://schemas.google.com/g/2005',
  41.     ),
  42.     array(
  43.         '_name' => 'atom:category',
  44.         '_attributes' => array(
  45.             'scheme' => 'http://schemas.google.com/g/2005#kind',
  46.             'term' => 'http://schemas.google.com/contact/2008#contact',
  47.         ),
  48.     ),
  49.     array(
  50.         '_name' => 'gd:name',
  51.         array(
  52.             '_name' => 'gd:givenName',
  53.             '_value' => 'Elizabeth',
  54.         ),
  55.         array(
  56.             '_name' => 'gd:familyName',
  57.             '_value' => 'Bennet',
  58.         ),
  59.         array(
  60.             '_name' => 'gd:fullName',
  61.             '_value' => 'Elizabeth Bennet',
  62.         ),
  63.     ),
  64.     array(
  65.         '_name' => 'atom:content',
  66.         '_attributes' => array(
  67.             'type' => 'text',
  68.         ),
  69.         '_value' => 'Notes',
  70.     ),
  71.     array(
  72.         '_name' => 'gd:email',
  73.         '_attributes' => array(
  74.             'rel' => 'http://schemas.google.com/g/2005#work',
  75.             'primary' => 'true',
  76.             'address' => 'liz@gmail.com',
  77.             'displayName' => 'E. Bennet',
  78.         ),
  79.     ),
  80.     array(
  81.         '_name' => 'gd:email',
  82.         '_attributes' => array(
  83.             'rel' => 'http://schemas.google.com/g/2005#home',
  84.             'address' => 'liz@example.org',
  85.         ),
  86.     ),
  87.     array(
  88.         '_name' => 'gd:phoneNumber',
  89.         '_attributes' => array(
  90.             'rel' => 'http://schemas.google.com/g/2005#work',
  91.             'primary' => 'true',
  92.         ),
  93.         '_value' => '(206)555-1212',
  94.     ),
  95.     array(
  96.         '_name' => 'gd:phoneNumber',
  97.         '_attributes' => array(
  98.             'rel' => 'http://schemas.google.com/g/2005#work',
  99.         ),
  100.         '_value' => '(206)555-1213',
  101.     ),
  102.     array(
  103.         '_name' => 'gd:im',
  104.         '_attributes' => array(
  105.             'address' => 'liz@gmail.com',
  106.             'protocol' => 'http://schemas.google.com/g/2005#GOOGLE_TALK',
  107.             'primary' => 'true',
  108.             'rel' => 'http://schemas.google.com/g/2005#home',
  109.         ),
  110.     ),
  111.     array(
  112.         '_name' => 'gd:structuredPostalAddress',
  113.         '_attributes' => array(
  114.             'rel' => 'http://schemas.google.com/g/2005#work',
  115.             'primary' => 'true',
  116.         ),
  117.         array(
  118.             '_name' => 'gd:city',
  119.             '_value' => 'Mountain View',
  120.         ),
  121.         array(
  122.             '_name' => 'gd:street',
  123.             '_value' => '1600 Amphitheatre Pkwy',
  124.         ),
  125.         array(
  126.             '_name' => 'gd:region',
  127.             '_value' => 'CA',
  128.         ),
  129.         array(
  130.             '_name' => 'gd:postcode',
  131.             '_value' => '94043',
  132.         ),
  133.         array(
  134.             '_name' => 'gd:country',
  135.             '_value' => 'United States',
  136.         ),
  137.         array(
  138.             '_name' => 'gd:formattedAddress',
  139.             '_value' => '1600 Amphitheatre Pkwy Mountain View',
  140.         ),
  141.     ),
  142. );
  143.  
  144.  
  145. $dom = new DOMDocument();
  146. $dom->formatOutput = true; // Pretty XML output
  147.  
  148. function generate_xml_element( $dom, $parent_element, $data ) {
  149.     $element = null;
  150.  
  151.     foreach ( $data as $data_key => $data_value ) {
  152.  
  153.         // "_name" contains the name of the current element
  154.         if ( '_name' === $data_key ) {
  155.             $element_value = ( ! empty( $data['_value'] ) ) ? $data['_value'] : null;
  156.             $element = $dom->createElement( $data_value, $element_value );
  157.         }
  158.  
  159.         // "_attributes" contains the attributes of the current element
  160.         elseif ( '_attributes' === $data_key && $element ) {
  161.             foreach ( $data_value as $attribute_key => $attribute_value ) {
  162.                 $element->setAttribute( $attribute_key, $attribute_value );
  163.             }
  164.         }
  165.  
  166.         // If the key is numeric, then it's a child element
  167.         elseif ( is_numeric( $data_key ) && $element ) {
  168.             $element->appendChild( generate_xml_element( $dom, $element, $data_value ) );
  169.         }
  170.  
  171.     }
  172.  
  173.     return $element;
  174. }
  175.  
  176. $dom->appendChild( generate_xml_element( $dom, $dom, $data ) );
  177.  
  178. echo '<pre>' . htmlspecialchars( $dom->saveXML() );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement