从数组创建XML

时间:2013-03-11 09:53:07

标签: php simplexml

我有这个数组

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"
  }
}

我想从上面的数组创建一个xml文件,我使用这段代码创建一个xml文件

$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");

        }
    }
}

此代码提供了像这样的mi解决方案

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

但我想要这样的结果

 <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>

如何在xml代码中添加额外的子代码?

1 个答案:

答案 0 :(得分:1)

在您的情况下,更改else部分:

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);
}

但正如Andrej建议的那样,你可能想看看更多的一般功能。

编辑:我的版本有效:

$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() );

输出:

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>

'(长度= 211)