如何将数组转换为SimpleXML

如何将数组转换为PHP中的SimpleXML对象?

485434 次浏览

如果数组是关联的并且键是正确的,那么首先将它转换为xml可能会更容易。喜欢的东西:

  function array2xml ($array_item) {
$xml = '';
foreach($array_item as $element => $value)
{
if (is_array($value))
{
$xml .= "<$element>".array2xml($value)."</$element>";
}
elseif($value == '')
{
$xml .= "<$element />";
}
else
{
$xml .= "<$element>".htmlentities($value)."</$element>";
}
}
return $xml;
}


$simple_xml = simplexml_load_string(array2xml($assoc_array));

另一种方法是首先创建基本的xml,例如

$simple_xml = simplexml_load_string("<array></array>");

然后对于数组的每个部分,使用类似于我的文本创建循环的东西,而不是使用simplexml函数“addChild”用于数组的每个节点。

稍后我将尝试使用这两个版本来更新这篇文章。

一个简短的例子:

<?php


$test_array = array (
'bla' => 'blub',
'foo' => 'bar',
'another_array' => array (
'stack' => 'overflow',
),
);
$xml = new SimpleXMLElement('<root/>');
array_walk_recursive($test_array, array ($xml, 'addChild'));
print $xml->asXML();

结果

<?xml version="1.0"?>
<root>
<blub>bla</blub>
<bar>foo</bar>
<overflow>stack</overflow>
</root>

键和值被交换——你可以在array_walk之前用array_flip()来修复这个问题。array_walk_recursive需要PHP 5。你可以用array_walk代替,但是你不会在xml中得到'stack' => 'overflow'

如果冗长的xml不是问题,可以使用xmlrpc_encode从数组创建xml。 www.php.net/xmlrpc_encode < / p >

注意,如果使用关联键和/或数字键,创建的XML会有所不同

<?php
// /params/param/value/struct/member
// there is a tag "member" for each element
// "member" contains a tag "name". its value is the associative key
$xml1 = xmlrpc_encode(array('a'=>'b','c'=>'d'));
$simplexml1 = simplexml_load_string($xml1);
print_r($xml1);
print_r($simplexml1);


// /params/param/value/array/data
// there is a tag "data" for each element
// "data" doesn't contain the tag "name"
$xml2 = xmlrpc_encode(array('a','b'));
$simplexml2 = simplexml_load_string($xml2);
print_r($xml2);
print_r($simplexml2);
?>
<?php
function array_to_xml(array $arr, SimpleXMLElement $xml)
{
foreach ($arr as $k => $v) {
is_array($v)
? array_to_xml($v, $xml->addChild($k))
: $xml->addChild($k, $v);
}
return $xml;
}


$test_array = array (
'bla' => 'blub',
'foo' => 'bar',
'another_array' => array (
'stack' => 'overflow',
),
);


echo array_to_xml($test_array, new SimpleXMLElement('<root/>'))->asXML();

下面是php 5.2代码,将数组的任何深度转换为xml文档:

Array
(
['total_stud']=> 500
[0] => Array
(
[student] => Array
(
[id] => 1
[name] => abc
[address] => Array
(
[city]=>Pune
[zip]=>411006
)
)
)
[1] => Array
(
[student] => Array
(
[id] => 2
[name] => xyz
[address] => Array
(
[city]=>Mumbai
[zip]=>400906
)
)


)
)

生成的XML如下:

<?xml version="1.0"?>
<student_info>
<total_stud>500</total_stud>
<student>
<id>1</id>
<name>abc</name>
<address>
<city>Pune</city>
<zip>411006</zip>
</address>
</student>
<student>
<id>1</id>
<name>abc</name>
<address>
<city>Mumbai</city>
<zip>400906</zip>
</address>
</student>
</student_info>
< p > PHP代码片段

<?php
// function defination to convert array to xml
function array_to_xml( $data, &$xml_data ) {
foreach( $data as $key => $value ) {
if( is_array($value) ) {
if( is_numeric($key) ){
$key = 'item'.$key; //dealing with <0/>..<n/> issues
}
$subnode = $xml_data->addChild($key);
array_to_xml($value, $subnode);
} else {
$xml_data->addChild("$key",htmlspecialchars("$value"));
}
}
}


// initializing or creating array
$data = array('total_stud' => 500);


// creating object of SimpleXMLElement
$xml_data = new SimpleXMLElement('<?xml version="1.0"?><data></data>');


// function call to convert array to xml
array_to_xml($data,$xml_data);


//saving generated xml file;
$result = $xml_data->asXML('/file/path/name.xml');


?>

在这个代码段中使用的SimpleXMLElement::asXML的文档 . 0

这是我的入口,简单而干净。

function array2xml($array, $xml = false){
if($xml === false){
$xml = new SimpleXMLElement('<root/>');
}
foreach($array as $key => $value){
if(is_array($value)){
array2xml($value, $xml->addChild($key));
}else{
$xml->addChild($key, $value);
}
}
return $xml->asXML();
}




header('Content-type: text/xml');
print array2xml($array);

这里提供的答案仅将数组转换为带节点的XML,不能设置属性。我已经编写了一个php函数,允许您将数组转换为php,并为xml中的特定节点设置属性。这里的缺点是您必须以很少约定的特定方式构造数组(仅当您想使用属性时)

下面的示例也允许您用XML设置属性。

来源: https://github.com/digitickets/lalit/blob/master/src/Array2XML.php < / p >

<?php
$books = array(
'@attributes' => array(
'type' => 'fiction'
),
'book' => array(
array(
'@attributes' => array(
'author' => 'George Orwell'
),
'title' => '1984'
),
array(
'@attributes' => array(
'author' => 'Isaac Asimov'
),
'title' => 'Foundation',
'price' => '$15.61'
),
array(
'@attributes' => array(
'author' => 'Robert A Heinlein'
),
'title' => 'Stranger in a Strange Land',
'price' => array(
'@attributes' => array(
'discount' => '10%'
),
'@value' => '$18.00'
)
)
)
);
/* creates
<books type="fiction">
<book author="George Orwell">
<title>1984</title>
</book>
<book author="Isaac Asimov">
<title>Foundation</title>
<price>$15.61</price>
</book>
<book author="Robert A Heinlein">
<title>Stranger in a Strange Land</title>
<price discount="10%">$18.00</price>
</book>
</books>
*/
?>

我使用了几个函数,我写了一会儿,以生成xml,从PHP和jQuery等来回传递… 也不使用任何额外的框架,只是单纯地生成一个字符串,然后可以与SimpleXML(或其他框架)使用

如果它对任何人有用,请使用它:)

function generateXML($tag_in,$value_in="",$attribute_in=""){
$return = "";
$attributes_out = "";
if (is_array($attribute_in)){
if (count($attribute_in) != 0){
foreach($attribute_in as $k=>$v):
$attributes_out .= " ".$k."=\"".$v."\"";
endforeach;
}
}
return "<".$tag_in."".$attributes_out.((trim($value_in) == "") ? "/>" : ">".$value_in."</".$tag_in.">" );
}


function arrayToXML($array_in){
$return = "";
$attributes = array();
foreach($array_in as $k=>$v):
if ($k[0] == "@"){
// attribute...
$attributes[str_replace("@","",$k)] = $v;
} else {
if (is_array($v)){
$return .= generateXML($k,arrayToXML($v),$attributes);
$attributes = array();
} else if (is_bool($v)) {
$return .= generateXML($k,(($v==true)? "true" : "false"),$attributes);
$attributes = array();
} else {
$return .= generateXML($k,$v,$attributes);
$attributes = array();
}
}
endforeach;
return $return;
}

爱所有人:)

只是对上面的函数进行了编辑,当键是数字时,添加前缀“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");
}
}
}
}
function array2xml($array, $xml = false){


if($xml === false){


$xml = new SimpleXMLElement('<?xml version=\'1.0\' encoding=\'utf-8\'?><'.key($array).'/>');
$array = $array[key($array)];


}
foreach($array as $key => $value){
if(is_array($value)){
$this->array2xml($value, $xml->addChild($key));
}else{
$xml->addChild($key, $value);
}
}
return $xml->asXML();
}

您可以直接在代码中使用以下函数,

    function artoxml($arr, $i=1,$flag=false){
$sp = "";
for($j=0;$j<=$i;$j++){
$sp.=" ";
}
foreach($arr as $key=>$val){
echo "$sp&lt;".$key."&gt;";
if($i==1) echo "\n";
if(is_array($val)){
if(!$flag){echo"\n";}
artoxml($val,$i+5);
echo "$sp&lt;/".$key."&gt;\n";
}else{
echo "$val"."&lt;/".$key."&gt;\n";
}
}


}

调用第一个参数作为数组的函数,第二个参数必须为1,这将增加完美缩进,第三个参数必须为真。

例如,如果要转换的数组变量是$array1,则, 调用时,调用函数应该封装在<pre>标记中

< >以前artoxml (array1美元,1,真的);< / >之前

执行文件后请查看页面源代码,因为<并且>符号不会显示在HTML页面中。

我的答案,拼凑别人的答案。这应该可以纠正无法补偿数字键的错误:

function array_to_xml($array, $root, $element) {
$xml = new SimpleXMLElement("<{$root}/>");
foreach ($array as $value) {
$elem = $xml->addChild($element);
xml_recurse_child($elem, $value);
}
return $xml;
}


function xml_recurse_child(&$node, $child) {
foreach ($child as $key=>$value) {
if(is_array($value)) {
foreach ($value as $k => $v) {
if(is_numeric($k)){
xml_recurse_child($node, array($key => $v));
}
else {
$subnode = $node->addChild($key);
xml_recurse_child($subnode, $value);
}
}
}
else {
$node->addChild($key, $value);
}
}
}

array_to_xml()函数假定数组首先由数字键组成。如果你的数组有一个初始元素,你会从array_to_xml()函数中删除foreach()$elem语句,只传递$xml

我会评论投票次数第二多的答案,因为如果有数字索引的内部数组,它不会保留结构并生成糟糕的xml。

我基于它开发了自己的版本,因为我需要简单的json和xml之间的转换器,而不管数据的结构。我的版本保留了数字键信息和原始数组的结构。它为数值索引值创建元素,方法是将值包装为值命名的元素,其key-attribute包含数值key。

例如

array('test' => array(0 => 'some value', 1 => 'other'))

皈依

<test><value key="0">some value</value><value key="1">other</value></test>

我的版本的array_to_xml -function(希望它能帮助到某人:)

function array_to_xml($arr, &$xml) {
foreach($arr as $key => $value) {
if(is_array($value)) {
if(!is_numeric($key)){
$subnode = $xml->addChild("$key");
} else {
$subnode = $xml->addChild("value");
$subnode->addAttribute('key', $key);
}
array_to_xml($value, $subnode);
}
else {
if (is_numeric($key)) {
$xml->addChild("value", $value)->addAttribute('key', $key);
} else {
$xml->addChild("$key",$value);
}
}
}
}

这里有一个函数帮我解决了这个问题:

就像这样叫它

echo arrayToXml("response",$arrayIWantToConvert);
function arrayToXml($thisNodeName,$input){
if(is_numeric($thisNodeName))
throw new Exception("cannot parse into xml. remainder :".print_r($input,true));
if(!(is_array($input) || is_object($input))){
return "<$thisNodeName>$input</$thisNodeName>";
}
else{
$newNode="<$thisNodeName>";
foreach($input as $key=>$value){
if(is_numeric($key))
$key=substr($thisNodeName,0,strlen($thisNodeName)-1);
$newNode.=arrayToXml3($key,$value);
}
$newNode.="</$thisNodeName>";
return $newNode;
}
}

我想要一个代码,将一个数组内的所有元素,并把它们作为属性,所有数组作为子元素。

对于像这样的东西

array (
'row1' => array ('head_element' =>array("prop1"=>"some value","prop2"=>array("empty"))),
"row2"=> array ("stack"=>"overflow","overflow"=>"overflow")
);

我会得到这样的结果

<?xml version="1.0" encoding="utf-8"?>
<someRoot>
<row1>
<head_element prop1="some value">
<prop2 0="empty"/>
</head_element>
</row1>
<row2 stack="overflow" overflow="stack"/>
</someRoot>

要实现这一点,代码如下,但要非常小心,它是递归的,实际上可能会导致stackoverflow:)

function addElements(&$xml,$array)
{
$params=array();
foreach($array as $k=>$v)
{
if(is_array($v))
addElements($xml->addChild($k), $v);
else $xml->addAttribute($k,$v);
}


}
function xml_encode($array)
{
if(!is_array($array))
trigger_error("Type missmatch xml_encode",E_USER_ERROR);
$xml=new SimpleXMLElement('<?xml version=\'1.0\' encoding=\'utf-8\'?><'.key($array).'/>');
addElements($xml,$array[key($array)]);
return $xml->asXML();
}

您可能希望添加数组长度检查,以便在数据部分中设置某些元素,而不是作为属性。

function toXML($data, $obj = false, $dom) {
$is_first_level = false;
if($obj === false) {
$dom = new DomDocument('1.0');
$obj = $dom;
$is_first_level = true;
}


if(is_array($data)) {
foreach($data as $key => $item) {
$this->toXML($item, $obj->appendChild($dom->createElement($key)), $dom);
}
}else {
$obj->appendChild($dom->createTextNode($data));
}


if($is_first_level) {
$obj->formatOutput = true;
return $obj->saveXML();
}
return $obj;
}

整个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();
}
}

如果你使用magento,你有这种类型的关联数组

$test_array = array (
'0' => array (
'category_id' => '582',
'name' => 'Surat',
'parent_id' => '565',
'child_id' => '567',
'active' => '1',
'level' => '6',
'position' => '17'
),


'1' => array (
'category_id' => '567',
'name' => 'test',
'parent_id' => '0',
'child_id' => '576',
'active' => '0',
'level' => '0',
'position' => '18'
),
);

那么最好将关联数组转换为XML格式。在控制器文件中使用此代码。

$this->loadLayout(false);
//header ("content-type: text/xml");
$this->getResponse()->setHeader('Content-Type','text/xml');
$this->renderLayout();


$clArr2xml = new arr2xml($test_array, 'utf-8', 'listdata');
$output = $clArr2xml->get_xml();
print $output;


class arr2xml
{
var $array = array();
var $xml = '';
var $root_name = '';
var $charset = '';


public function __construct($array, $charset = 'utf-8', $root_name = 'root')
{
header ("content-type: text/xml");
$this->array = $array;
$this->root_name = $root_name;
$this->charset = $charset;


if (is_array($array) && count($array) > 0) {
$this->struct_xml($array);


} else {
$this->xml .= "no data";
}
}


public function struct_xml($array)
{
foreach ($array as $k => $v) {
if (is_array($v)) {
$tag = ereg_replace('^[0-9]{1,}', 'item', $k); // replace numeric key in array to 'data'
$this->xml .= "<$tag>";
$this->struct_xml($v);
$this->xml .= "</$tag>";
} else {
$tag = ereg_replace('^[0-9]{1,}', 'item', $k); // replace numeric key in array to 'data'
$this->xml .= "<$tag><![CDATA[$v]]></$tag>";
}
}
}


public function get_xml()
{


$header = "<?xml version=\"1.0\" encoding=\"" . $this->charset . "\"?><" . $this->root_name . ">";
$footer = "</" . $this->root_name . ">";


return $header . $this->xml . $footer;
}
}

我希望这对大家都有帮助。

总之……我使用了onokazu的代码(谢谢!),并添加了在XML中重复标记的能力,它还支持属性,希望有人发现它有用!

 <?php


function array_to_xml(array $arr, SimpleXMLElement $xml) {
foreach ($arr as $k => $v) {


$attrArr = array();
$kArray = explode(' ',$k);
$tag = array_shift($kArray);


if (count($kArray) > 0) {
foreach($kArray as $attrValue) {
$attrArr[] = explode('=',$attrValue);
}
}


if (is_array($v)) {
if (is_numeric($k)) {
array_to_xml($v, $xml);
} else {
$child = $xml->addChild($tag);
if (isset($attrArr)) {
foreach($attrArr as $attrArrV) {
$child->addAttribute($attrArrV[0],$attrArrV[1]);
}
}
array_to_xml($v, $child);
}
} else {
$child = $xml->addChild($tag, $v);
if (isset($attrArr)) {
foreach($attrArr as $attrArrV) {
$child->addAttribute($attrArrV[0],$attrArrV[1]);
}
}
}
}


return $xml;
}


$test_array = array (
'bla' => 'blub',
'foo' => 'bar',
'another_array' => array (
array('stack' => 'overflow'),
array('stack' => 'overflow'),
array('stack' => 'overflow'),
),
'foo attribute1=value1 attribute2=value2' => 'bar',
);


$xml = array_to_xml($test_array, new SimpleXMLElement('<root/>'))->asXML();


echo "$xml\n";
$dom = new DOMDocument;
$dom->preserveWhiteSpace = FALSE;
$dom->loadXML($xml);
$dom->formatOutput = TRUE;
echo $dom->saveXml();
?>

我发现所有的答案都是使用过多的代码。这里有一个简单的方法:

function to_xml(SimpleXMLElement $object, array $data)
{
foreach ($data as $key => $value) {
if (is_array($value)) {
$new_object = $object->addChild($key);
to_xml($new_object, $value);
} else {
// if the key is an integer, it needs text with it to actually work.
if ($key != 0 && $key == (int) $key) {
$key = "key_$key";
}


$object->addChild($key, $value);
}
}
}

然后,将数组发送到使用递归的函数中就很简单了,因此它将处理多维数组:

$xml = new SimpleXMLElement('<rootTag/>');
to_xml($xml, $my_array);

现在$xml包含了一个漂亮的xml对象,它完全基于您编写数组的方式。

print $xml->asXML();

// 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('<?xml version=\"1.0\"?><student_info></student_info>');




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

你可以使用xmlparse,我一直在工作。

$xml = XMLParser::encode(array(
'bla' => 'blub',
'foo' => 'bar',
'another_array' => array (
'stack' => 'overflow',
)
));
// @$xml instanceof SimpleXMLElement
echo $xml->asXML();

会导致:

<?xml version="1.0"?>
<root>
<bla>blub</bla>
<foo>bar</foo>
<another_array>
<stack>overflow</stack>
</another_array>
</root>

我发现这个解与原来的问题相似

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

另一个改进:

/**
* Converts an array to XML
*
* @param array $array
* @param SimpleXMLElement $xml
* @param string $child_name
*
* @return SimpleXMLElement $xml
*/
public function arrayToXML($array, SimpleXMLElement $xml, $child_name)
{
foreach ($array as $k => $v) {
if(is_array($v)) {
(is_int($k)) ? $this->arrayToXML($v, $xml->addChild($child_name), $v) : $this->arrayToXML($v, $xml->addChild(strtolower($k)), $child_name);
} else {
(is_int($k)) ? $xml->addChild($child_name, $v) : $xml->addChild(strtolower($k), $v);
}
}


return $xml->asXML();
}

用法:

$this->arrayToXML($array, new SimpleXMLElement('<root/>'), 'child_name_to_replace_numeric_integers');

以上答案大部分是正确的。然而,我想出了这个答案,它解决了array_walk_recursive的兼容性问题,也解决了数值键的问题。它也通过了我做的所有测试:

function arrayToXML(Array $array, SimpleXMLElement &$xml) {


foreach($array as $key => $value) {


// None array
if (!is_array($value)) {
(is_numeric($key)) ? $xml->addChild("item$key", $value) : $xml->addChild($key, $value);
continue;
}


// Array
$xmlChild = (is_numeric($key)) ? $xml->addChild("item$key") : $xml->addChild($key);
arrayToXML($value, $xmlChild);
}
}

我还为此添加了一个测试类,你可能会发现有用:

class ArrayToXmlTest extends PHPUnit_Framework_TestCase {


public function setUp(){ }
public function tearDown(){ }


public function testFuncExists() {
$this->assertTrue(function_exists('arrayToXML'));
}


public function testFuncReturnsXml() {
$array = array(
'name' => 'ardi',
'last_name' => 'eshghi',
'age' => 31,
'tel' => '0785323435'
);


$xmlEl =  new SimpleXMLElement('<root/>');
arrayToXml($array, $xmlEl);


$this->assertTrue($xmlEl instanceOf SimpleXMLElement);
}


public function testAssocArrayToXml() {


$array = array(
'name' => 'ardi',
'last_name' => 'eshghi',
'age' => 31,
'tel' => '0785323435'
);


$expectedXmlEl = new SimpleXMLElement('<root/>');
$expectedXmlEl->addChild('name', $array['name']);
$expectedXmlEl->addChild('last_name', $array['last_name']);
$expectedXmlEl->addChild('age', $array['age']);
$expectedXmlEl->addChild('tel', $array['tel']);


$actualXmlEl =  new SimpleXMLElement('<root/>');
arrayToXml($array, $actualXmlEl);


$this->assertEquals($expectedXmlEl->asXML(), $actualXmlEl->asXML());
}


public function testNoneAssocArrayToXml() {


$array = array(
'ardi',
'eshghi',
31,
'0785323435'
);


// Expected xml value
$expectedXmlEl = new SimpleXMLElement('<root/>');
foreach($array as $key => $value)
$expectedXmlEl->addChild("item$key", $value);


// What the function produces
$actualXmlEl =  new SimpleXMLElement('<root/>');
arrayToXml($array, $actualXmlEl);


$this->assertEquals($expectedXmlEl->asXML(), $actualXmlEl->asXML());
}


public function testNestedMixArrayToXml() {


$testArray = array(
"goal",
"nice",
"funny" => array(
'name' => 'ardi',
'tel'   =>'07415517499',
"vary",
"fields" => array(
'small',
'email' => 'ardi.eshghi@gmail.com'
),


'good old days'


),


"notes" => "come on lads lets enjoy this",
"cast" => array(
'Tom Cruise',
'Thomas Muller' => array('age' => 24)
)
);


// Expected xml value
$expectedXmlEl = new SimpleXMLElement('<root/>');
$expectedXmlEl->addChild('item0', $testArray[0]);
$expectedXmlEl->addChild('item1', $testArray[1]);
$childEl = $expectedXmlEl->addChild('funny');
$childEl->addChild("name", $testArray['funny']['name']);
$childEl->addChild("tel", $testArray['funny']['tel']);
$childEl->addChild("item0", "vary");
$childChildEl = $childEl->addChild("fields");
$childChildEl->addChild('item0', 'small');
$childChildEl->addChild('email', $testArray['funny']['fields']['email']);
$childEl->addChild("item1", 'good old days');
$expectedXmlEl->addChild('notes', $testArray['notes']);
$childEl2 = $expectedXmlEl->addChild('cast');
$childEl2->addChild('item0', 'Tom Cruise');
$childChildEl2 = $childEl2->addChild('Thomas Muller');
$childChildEl2->addChild('age', $testArray['cast']['Thomas Muller']['age']);


// What the function produces
$actualXmlEl = new SimpleXMLElement('<root/>');
arrayToXml($testArray, $actualXmlEl);


$this->assertEquals($expectedXmlEl->asXML(), $actualXmlEl->asXML());
}
}

基于这里的所有内容,通过@前缀处理数值索引+属性,并可以将xml注入到现有节点:

代码

function simple_xmlify($arr, SimpleXMLElement $root = null, $el = 'x') {
// based on, among others http://stackoverflow.com/a/1397164/1037948


if(!isset($root) || null == $root) $root = new SimpleXMLElement('<' . $el . '/>');


if(is_array($arr)) {
foreach($arr as $k => $v) {
// special: attributes
if(is_string($k) && $k[0] == '@') $root->addAttribute(substr($k, 1),$v);
// normal: append
else simple_xmlify($v, $root->addChild(
// fix 'invalid xml name' by prefixing numeric keys
is_numeric($k) ? 'n' . $k : $k)
);
}
} else {
$root[0] = $arr;
}


return $root;
}//--   fn  simple_xmlify

使用

// lazy declaration via "queryparam"
$args = 'hello=4&var[]=first&var[]=second&foo=1234&var[5]=fifth&var[sub][]=sub1&var[sub][]=sub2&var[sub][]=sub3&var[@name]=the-name&var[@attr2]=something-else&var[sub][@x]=4.356&var[sub][@y]=-9.2252';
$q = array();
parse_str($val, $q);


$xml = simple_xmlify($q); // dump $xml, or...
$result = get_formatted_xml($xml); // see below

结果

<?xml version="1.0"?>
<x>
<hello>4</hello>
<var name="the-name" attr2="something-else">
<n0>first</n0>
<n1>second</n1>
<n5>fifth</n5>
<sub x="4.356" y="-9.2252">
<n0>sub1</n0>
<n1>sub2</n1>
<n2>sub3</n2>
</sub>
</var>
<foo>1234</foo>
</x>

好处:格式化XML

function get_formatted_xml(SimpleXMLElement $xml, $domver = null, $preserveWhitespace = true, $formatOutput = true) {
// http://stackoverflow.com/questions/1191167/format-output-of-simplexml-asxml


// create new wrapper, so we can get formatting options
$dom = new DOMDocument($domver);
$dom->preserveWhiteSpace = $preserveWhitespace;
$dom->formatOutput = $formatOutput;
// now import the xml (converted to dom format)
/*
$ix = dom_import_simplexml($xml);
$ix = $dom->importNode($ix, true);
$dom->appendChild($ix);
*/
$dom->loadXML($xml->asXML());


// print
return $dom->saveXML();
}//--   fn  get_formatted_xml

下面讨论名称空间。在本例中,构造包装器以包含名称空间定义,并将其传递给函数。使用冒号来标识命名空间。

测试数组

$inarray = [];
$inarray['p:apple'] = "red";
$inarray['p:pear'] = "green";
$inarray['p:peach'] = "orange";
$inarray['p1:grocers'] = ['p1:local' => "cheap", 'p1:imported' => "expensive"];




$xml = new SimpleXMLElement( '<p:wrapper xmlns:p="http://namespace.org/api" xmlns:p1="http://namespace.org/api2 /> ');


array_to_xml($xml,$inarray);








function array_to_xml(SimpleXMLElement $object, array $data)
{
$nslist = $object->getDocNamespaces();


foreach ($data as $key => $value)
{
$nspace = null;
$keyparts = explode(":",$key,2);
if ( count($keyparts)==2)
$nspace = $nslist[$keyparts[0]];


if (is_array($value))
{
$key = is_numeric($key) ? "item$key" : $key;
$new_object = $object->addChild($key,null,$nspace);
array_to_xml($new_object, $value);
}
else
{
$key = is_numeric($key) ? "item$key" : $key;
$object->addChild($key, $value,$nspace);
}
}
}

来自PHP 5.4

function array2xml($data, $root = null){
$xml = new SimpleXMLElement($root ? '<' . $root . '/>' : '<root/>');
array_walk_recursive($data, function($value, $key)use($xml){
$xml->addChild($key, $value);
});
return $xml->asXML();
}

其他解决方案:

$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);
function array2xml(array $data, SimpleXMLElement $object = null, $oldNodeName = 'item')
{
if (is_null($object)) $object = new SimpleXMLElement('<root/>');
$isNumbered = true;
$idx = 0;
foreach ($data as $key => $x)
if (is_string($key) || ($idx++ != $key + 0))
$isNumbered = false;
foreach ($data as $key => $value)
{
$attribute = preg_match('/^[0-9]/', $key . '') ? $key : null;
$key = (is_string($key) && !preg_match('/^[0-9]/', $key . '')) ? $key : preg_replace('/s$/', '', $oldNodeName);
if (is_array($value))
{
$new_object = $object->addChild($key);
if (!$isNumbered && !is_null($attribute)) $new_object->addAttribute('id', $attribute);
array2xml($value, $new_object, $key);
}
else
{
if (is_bool($value)) $value = $value ? 'true' : 'false';
$node = $object->addChild($key, htmlspecialchars($value));
if (!$isNumbered && !is_null($attribute) && !isset($node->attributes()->id))
$node->addAttribute('id', $attribute);
}
}
return $object;
}

例如,这个函数返回一个< object >…</ object >< object >…用于数值索引的XML标记。

输入:

    array(
'people' => array(
'dog',
'cat',
'life' => array(
'gum',
'shoe',
),
'fish',
),
array('yeah'),
)

输出:

<root>
<people>
<people>dog</people>
<people>cat</people>
<life>
<life>gum</life>
<life>shoe</life>
</life>
<people>fish</people>
<people>
<people>yeah</people>
</people>
</people>
</root>

这应该能满足所有的共同需求。也许你可以把第三行改成:

$key = is_string($key) ? $key : $oldNodeName . '_' . $key;

或者如果你正在处理以s结尾的复数:

$key = is_string($key) ? $key : preg_replace('/s$/', '', $oldNodeName);

使用< >强FluidXML < / >强,你可以从PHP数组开始,为SimpleXML生成XML,包含…只有两行代码。

$fluidxml  = fluidxml($array);
$simplexml = simplexml_import_dom($fluidxml->dom());

一个示例数组可以是

$array = [ 'doc' => [
'fruit' => 'orange',
'cake'  => [
'@id' => '123',
'@'   => 'tiramisu' ],
[ 'pasta' => 'matriciana' ],
[ 'pasta' => 'boscaiola'  ]
] ];

https://github.com/servo-php/fluidxml

您也可以通过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);
}
}


?>

你可以使用胡子模板引擎,并制作一个模板,如:

\{\{#RECEIVER}}
<RECEIVER>
<COMPANY>\{\{{COMPANY}}}</COMPANY>
<CONTACT>\{\{{CONTACT}}}</CONTACT>
<ADDRESS>\{\{{ADDRESS}}}</ADDRESS>
<ZIP>\{\{ZIP}}</ZIP>
<CITY>\{\{{CITY}}}</CITY>
</RECEIVER>
\{\{/RECEIVER}}
\{\{#DOC}}
<DOC>
<TEXT>\{\{{TEXT}}}</TEXT>
<NUMBER>\{\{{NUMBER}}}</NUMBER>
</DOC>
\{\{/DOC}}

在PHP中这样使用它:

require_once( __DIR__ .'/../controls/Mustache/Autoloader.php' );
Mustache_Autoloader::register();
$oMustache = new Mustache_Engine();
$sTemplate = implode( '', file( __DIR__ ."/xml.tpl" ));
$return = $oMustache->render($sTemplate, $res);
echo($return);

我认为以上所有的解决方案都很好,但我到目前为止看到的,它并没有真正创建一个准确的格式良好的XML,因为数组键与$my_array[main_node][multiple_values][] = ARRAY ('id' =>然后将'1')转换为

    <main_node>
<multiple_values>
<0>
<id>1 test</id>
</0>
</multiple_values>
<multiple_values>
<1>
<id>2 test</id>
</1>
</multiple_values>
</main_node>

这是XML解析器方面的一个问题……

I should be like this:


<main_node>
<multiple_values>
<id>1 test</id>
</multiple_values>
<multiple_values>
<id>2 test</id>
</multiple_values>
</main_node>

如果你用load_simple_xml来解析…你会得到完全相同的数组/对象结构。

我的函数还自动创建正确的根节点。

    // Code to convert php array to xml document 20211112
function array2xml(array $data, $xml_class_obj = '', $group_by_parent_allowed = '', $options = array())
{
            

if(!$xml_class_obj) :
$is_root = 1;
$xml_class_obj = new XMLWriter();
$xml_class_obj->openMemory();
$xml_class_obj->setIndent(TRUE);
$xml_class_obj->setIndentString('   ');
if($options['encoding'] != '')  $xml_class_obj->startDocument('1.0', $options['encoding']);
else                                                        $xml_class_obj->startDocument('1.0');
endif;
            

foreach ($data as $key => $value) {


if (is_array($value)) { // IS ARRAY


// check if allow below keys are int, if yes group them to same parent tree
$group_by_parent = $key;
foreach(array_keys($value) as $c_keys) :
if(!is_int($c_keys)) $group_by_parent = '';
endforeach;


if(empty($group_by_parent)) $xml_class_obj->startElement($key);
if($group_by_parent_allowed != '') $xml_class_obj->startElement($group_by_parent_allowed);
                    

$this->array2xml($value, $xml_class_obj, $group_by_parent, $options);
                    

if(empty($group_by_parent)) $xml_class_obj->endElement();


} else { // IS VALUE
                    

if(is_string($value)) :
$xml_class_obj->startElement($key);
$xml_class_obj->writeCData($value);
$xml_class_obj->endElement();
else :
$xml_class_obj->writeElement($key, $value);
endif;


}


} // foreach
            

if($group_by_parent_allowed != '')  $xml_class_obj->endElement();
            

if($is_root == 1) :
            

$xml_class_obj->endDocument();
return $xml_class_obj->outputMemory();
else :
return $xml_class_obj;
endif;
              

}
    

// usage
$ary_new_xml = array();
$ary_new_xml['order']['customer']['customerid'] = '123456';
$ary_new_xml['order']['customer']['customertype'] = 15;
            

$ary_new_xml['order']['orderprio'] = 2;
            

$ary_new_xml['order']['orderpos'][] = array('sku' => 9999910001111, 'quantity' => 3);
$ary_new_xml['order']['orderpos'][] = array('sku' => 9999910002222, 'quantity' => 1);
            

echo array2xml($ary_new_xml,'','',array('enconding' => 'UTF-8'));

结果:

<?xml version="1.0" encoding="UTF-8"?>
<order>
<customer>
<customerid>82936639</customerid>
<customertype>15</customertype>
</customer>
<orderprio>2</orderprio>
<orderpos>
<sku>9999910001111</sku>
<quantity>3</quantity>
</orderpos>
<orderpos>
<sku>9999910002222</sku>
<quantity>1</quantity>
</orderpos>
</order>




 

我希望这能帮助到一些人;)