Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create XML from an array

Tags:

php

simplexml

I have this array

array(2) {
  [0] =>
  array(3) {
    ['name'] => string(4) "John"
    ['lastname'] => string(3) "Don"
    ['pesel'] => string(6) "987987"
  }
  [1] =>
  array(3) {
    ['name'] => string(4) "Mike"
    ['lastname'] => string(5) "Evans"
    ['pesel'] => string(5) "89779"
  }
}

And I want create a xml file from array above, I use this code to create a xml file

$student_info = array($resultant_array);

// creating object of SimpleXMLElement
$xml_student_info = new SimpleXMLElement("<?xml version=\"1.0\"?><student_info></student_info>");

// function call to convert array to xml
array_to_xml($student_info,$xml_student_info);

//saving generated xml file
$xml_student_info->asXML('inxfo.xml');



// function defination to convert array to xml
function array_to_xml($student_info, $xml_student_info) {
    foreach($student_info as $key => $value) {
            var_dump($value);
            echo '<br />';
        if(is_array($value)) {


            if(!is_numeric($key)){

                $subnode = $xml_student_info->addChild("$key");
                array_to_xml($value, $subnode);

            }
            else{

                array_to_xml($value, $xml_student_info);
            }
        }
        else { 

            $xml_student_info->addChild("$key","$value");

        }
    }
}

This code give mi solution like this

<student_info>
  <name>John</name>
  <lastname>Dozzn</lastname>
  <pesel>987987</pesel>
  <nme>Mike</name>
  <lastname>Evans</lastname>
  <pesel>89779</pesel>
</student_info>

But I want result like this

 <student_info>
      <person>
         <name>John</name>
         <lastname>Dozzn</lastname>
         <pesel>987987</pesel>   
      </person>
      <person>
         <name>Mike</name>         
         <lastname>Evans</lastname>
         <pesel>89779</pesel>
    </person>
 </student_info>

How can I add additional child to my xml code ?

like image 406
konadrian Avatar asked Apr 22 '26 21:04

konadrian


1 Answers

In your case, change the else part:

if(!is_numeric($key)){
     $subnode = $xml_student_info->addChild("$key");
     array_to_xml($value, $subnode);
}
else{
     $subnode = $xml_student_info->addChild("person");
     array_to_xml($value, $subnode);
}

But as Andrej suggested, you probably want to look at more general functions.

EDIT: My version that works:

$student_info = array(
    array(
        'name' => "John",
        'lastname' => "Don",
        'pesel' => "987987",
    ),
    array(
        'name' => "Mike",
        'lastname' => "Evans",
        'pesel' => "89779",
    )
);

$xml_student_info = new SimpleXMLElement("<?xml version=\"1.0\"?><student_info></student_info>");

function array_to_xml($student_info, $xml_student_info) {
    foreach($student_info as $key => $value) {
        if(is_array($value)) {
            if(!is_numeric($key)){
                $subnode = $xml_student_info->addChild("$key");
                array_to_xml($value, $subnode);
            }
            else{
                $subnode = $xml_student_info->addChild("person");
                array_to_xml($value, $subnode);
            }
        }
        else { 
            $xml_student_info->addChild("$key","$value");
        }
    }
}

array_to_xml($student_info, $xml_student_info);

var_dump( $xml_student_info->asXML() );

Outputs:

string '<?xml version="1.0"?>
<student_info><person><name>John</name><lastname>Don</lastname><pesel>987987</pesel></person><person><name>Mike</name><lastname>Evans</lastname><pesel>89779</pesel></person></student_info>

' (length=211)

like image 187
Scott Yang Avatar answered Apr 24 '26 10:04

Scott Yang



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!