如何将数组转换为PHP中的SimpleXML对象?
当前回答
您也可以通过DOM来实现这一点。请参阅下面的代码。
<?php
$el = array();
$command = array();
$dom = new DOMDocument('1.0', 'utf-8');
$dom->formatOutput = true;
$xml_array = [
'root'=>[
'Good guy' => [
'name' => [
'_cdata' => 'Luke Skywalker'
],
'weapon' => 'Lightsaber'
],
'Bad guy' => [
'name' => 'Sauron',
'weapon' => 'Evil Eye'
]
]
];
convert_xml($xml_array);
if(!empty($el))
{
$dom->appendChild(end($el));
}
echo $dom->saveXML();
?>
<?php
function convert_xml($Xml)
{
global $el, $dom;
foreach($Xml as $id=>$val)
{
if(is_numeric($id))
{
$id = "Item".($id);
}
$id = str_replace(' ', '-', strtolower($id));
if(is_array($val))
{
$ele = $dom->createElement($id);
array_push($el, $ele);
convert_xml($val);
}
else
{
$ele = $dom->createElement($id, $val);
if(!empty($el))
{
$com = end($el)->appendChild($ele);
}
else
{
$dom->appendChild($ele);
}
}
}
if(sizeof($el) > 1)
{
$child = end($el);
$com = prev($el)->appendChild($child);
array_pop($el);
}
}
?>
其他回答
其他解决方案:
$marray=array(....);
$options = array(
"encoding" => "UTF-8",
"output_type" => "xml",
"version" => "simple",
"escaping" => array("non-ascii, on-print, markup")
);
$xmlres = xmlrpc_encode_request('root', $marray, $options);
print($xmlres);
整个XML结构定义在$data数组中:
function array2Xml($data, $xml = null)
{
if (is_null($xml)) {
$xml = simplexml_load_string('<' . key($data) . '/>');
$data = current($data);
$return = true;
}
if (is_array($data)) {
foreach ($data as $name => $value) {
array2Xml($value, is_numeric($name) ? $xml : $xml->addChild($name));
}
} else {
$xml->{0} = $data;
}
if (!empty($return)) {
return $xml->asXML();
}
}
只是对上面的函数进行了编辑,当键是数字时,添加前缀“key_”
// initializing or creating array
$student_info = array(your array data);
// 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,$xml_student_info);
//saving generated xml file
$xml_student_info->asXML('file path and name');
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("key_$key");
array_to_xml($value, $subnode);
}
}
else {
if(!is_numeric($key)){
$xml_student_info->addChild("$key","$value");
}else{
$xml_student_info->addChild("key_$key","$value");
}
}
}
}
我发现这个解与原来的问题相似
<?php
$test_array = array (
'bla' => 'blub',
'foo' => 'bar',
'another_array' => array (
'stack' => 'overflow',
),
);
class NoSimpleXMLElement extends SimpleXMLElement {
public function addChild($name,$value) {
parent::addChild($value,$name);
}
}
$xml = new NoSimpleXMLElement('<root/>');
array_walk_recursive($test_array, array ($xml, 'addChild'));
print $xml->asXML();
您可以直接在代码中使用以下函数,
function artoxml($arr, $i=1,$flag=false){
$sp = "";
for($j=0;$j<=$i;$j++){
$sp.=" ";
}
foreach($arr as $key=>$val){
echo "$sp<".$key.">";
if($i==1) echo "\n";
if(is_array($val)){
if(!$flag){echo"\n";}
artoxml($val,$i+5);
echo "$sp</".$key.">\n";
}else{
echo "$val"."</".$key.">\n";
}
}
}
调用第一个参数作为数组的函数,第二个参数必须为1,这将增加完美缩进,第三个参数必须为真。
例如,如果要转换的数组变量是$array1,那么, 调用时,调用函数应该封装在<pre>标记中。
Artoxml($array 1,1,true);
请在执行文件后查看页面源代码,因为<和>符号将不会显示在html页面中。