How to convert array to SimpleXML

前端 未结 30 2659
遇见更好的自我
遇见更好的自我 2020-11-21 06:52

How can I convert an array to a SimpleXML object in PHP?

30条回答
  •  我寻月下人不归
    2020-11-21 07:10

    // Structered array for XML convertion.
    $data_array = array(
      array(
        '#xml_tag' => 'a',
        '#xml_value' => '',
        '#tag_attributes' => array(
          array(
            'name' => 'a_attr_name',
            'value' => 'a_attr_value',
          ),
        ),
        '#subnode' => array(
          array(
            '#xml_tag' => 'aa',
            '#xml_value' => 'aa_value',
            '#tag_attributes' => array(
              array(
                'name' => 'aa_attr_name',
                'value' => 'aa_attr_value',
              ),
            ),
            '#subnode' => FALSE,
          ),
        ),
      ),
      array(
        '#xml_tag' => 'b',
        '#xml_value' => 'b_value',
        '#tag_attributes' => FALSE,
        '#subnode' => FALSE,
      ),
      array(
        '#xml_tag' => 'c',
        '#xml_value' => 'c_value',
        '#tag_attributes' => array(
          array(
            'name' => 'c_attr_name',
            'value' => 'c_attr_value',
          ),
          array(
            'name' => 'c_attr_name_1',
            'value' => 'c_attr_value_1',
          ),
        ),
        '#subnode' => array(
          array(
            '#xml_tag' => 'ca',  
            '#xml_value' => 'ca_value',
            '#tag_attributes' => FALSE,
            '#subnode' => array(
              array(
                '#xml_tag' => 'caa',
                '#xml_value' => 'caa_value',
                '#tag_attributes' => array(
                  array(
                    'name' => 'caa_attr_name',
                    'value' => 'caa_attr_value',
                  ),
                ),
                '#subnode' => FALSE,
              ),
            ),
          ),
        ),
      ),
    );
    
    
    // creating object of SimpleXMLElement
    $xml_object = new SimpleXMLElement('');
    
    
    // function call to convert array to xml
    array_to_xml($data_array, $xml_object);
    
    // saving generated xml file
    $xml_object->asXML('/tmp/test.xml');
    
    /**
     * Converts an structured PHP array to XML.
     *
     * @param Array $data_array
     *   The array data for converting into XML.
     * @param Object $xml_object
     *   The SimpleXMLElement Object
     *
     * @see https://gist.github.com/drupalista-br/9230016
     * 
     */
    function array_to_xml($data_array, &$xml_object) {
      foreach($data_array as $node) {
        $subnode = $xml_object->addChild($node['#xml_tag'], $node['#xml_value']);
    
        if ($node['#tag_attributes']) {
          foreach ($node['#tag_attributes'] as $tag_attributes) {
            $subnode->addAttribute($tag_attributes['name'], $tag_attributes['value']); 
          }
        }
    
        if ($node['#subnode']) {
          array_to_xml($node['#subnode'], $subnode);
        }
      }
    }
    

提交回复
热议问题