将PHP数据结构转换为序列化XML

时间:2016-05-15 13:12:32

标签: php xml xml-serialization

我有以下数组。我甚至不确定该阵列是否格式正确。我甚至不确定我的阵列是否正确。

我想使用PHP将以下数组转换为序列化XML。我正在使用attr标签来获取属性。

这是数组:

$data = Array(
'name' => 'account',
'attr' => Array(
    'id' => 123456
),
'children' => Array(
    Array(
        'name' => 'name',
        'attr' => Array(),
        'children' => Array(
            'BBC'
        ),
    ),
    Array(
        'name' => 'monitors',
            'attr' => Array(),
        'children' => Array(
            Array(
                'name' => 'monitor',
                'attr' => Array(
                    'id' => 5235632
                ),
                'children' => Array(
                    Array(
                        'name' => 'url',
                        'attr' => Array(),
                        'children' => Array(
                            'http://www.bbc.co.uk/'
                        )
                    )
                )
            ),
            Array(
                'name' => 'monitor',
                'attr' => Array(
                    'id' => 5235633
                ),
                'children' => Array(
                    Array(
                        'name' => 'url',
                        'attr' => Array(),
                        'children' => Array(
                            'http://www.bbc.co.uk/news'
                        )
                    )
                )
            )
        )
    )
)
);

3 个答案:

答案 0 :(得分:1)

尝试以下功能

function assocArrayToXML($root_element_name,$ar)
{
    $xml = new SimpleXMLElement("<?xml version=\"1.0\"?><{$root_element_name}></{$root_element_name}>");
    $f = function($f,$c,$a) {
            foreach($a as $k=>$v) {
                if(is_array($v)) {
                    $ch=$c->addChild($k);
                    $f($f,$ch,$v);
                } else {
                    $c->addChild($k,$v);
                }
            }
    };
    $f($f,$xml,$ar);
    return $xml->asXML();
} 


echo assocArrayToXML("root",$data);

test it here

答案 1 :(得分:1)

使用递归函数非常容易。您的基本数组包含3个元素,名称,属性列表和子元素。因此,您的函数必须创建并附加具有名称的节点,设置所有属性并迭代子数据。如果子节点是标量,则它是一个文本节点,对于数组调用函数本身。

function appendTo($parent, $data) {
  $document = $parent->ownerDocument ?: $parent; 
  $node = $parent->appendChild($document->createElement($data['name']));
  if (isset($data['attr']) && is_array($data['attr'])) {
    foreach ($data['attr'] as $name => $value) {
      $node->setAttribute($name, $value);
    }
  }  
  if (isset($data['children']) && is_array($data['children'])) {
    foreach ($data['children'] as $name => $childData) {
      if (is_scalar($childData)) {
        $node->appendChild($document->createTextNode($childData));
      } elseif (is_array($childData)) {
        appendTo($node, $childData);
      }
    }
  }
}

$document = new DOMDocument();
$document->formatOutput = TRUE;
appendTo($document, $data);

echo $document->saveXml();

输出:

<?xml version="1.0"?>
<account id="123456">
  <name>BBC</name>
  <monitors>
    <monitor id="5235632">
      <url>http://www.bbc.co.uk/</url>
    </monitor>
    <monitor id="5235633">
      <url>http://www.bbc.co.uk/news</url>
    </monitor>
  </monitors>
</account>

答案 2 :(得分:0)

希望这会有所帮助。

<?php
function array2xml($arr)
{
    $dom = new DomDocument('1.0');
    /*
    *Create Root
    */  
    $root = $dom->createElement($arr['name']);
    if(isset($arr['attr']) && !empty($arr['attr']))
    {
        foreach($arr['attr'] as $key=>$val)
        $root->setAttribute($key, $val); 
    }
    $root = $dom->appendChild($root);
    createChilds($arr['children'], $dom, $root);

    header('Content-type: text/xml');
    echo $dom->saveXML(); 
}
function createChilds($arr, $dom, $parent)
{
    foreach($arr as $child)
    {
        if(isset($child['name']))
            $node = $dom->createElement($child['name']);
        /*
        *Add Attributes
        */
        if(isset($child['attr']) && !empty($child['attr']))
        {
            foreach($child['attr'] as $key=>$val)
            $node->setAttribute($key, $val); 
        }
        /*
        *Add Childs Recursively
        */      
        if(isset($child['children']) && is_array($child['children']))
        {
            createChilds($child['children'], $dom, $node);
        }
        else if(isset($child) && is_string($child))
        {
            $text = $dom->createTextNode($child); 
            $parent->appendChild($text); 
        }
        if(isset($node))
        $parent->appendChild($node);
    }
}

$data = Array(
'name' => 'account',
'attr' => Array(
    'id' => 123456
),
'children' => Array(
    Array(
        'name' => 'name',
        'attr' => Array(),
        'children' => Array(
            'BBC'
        ),
    ),
    Array(
        'name' => 'monitors',
            'attr' => Array(),
        'children' => Array(
            Array(
                'name' => 'monitor',
                'attr' => Array(
                    'id' => 5235632
                ),
                'children' => Array(
                    Array(
                        'name' => 'url',
                        'attr' => Array(),
                        'children' => Array(
                            'http://www.bbc.co.uk/'
                        )
                    )
                )
            ),
            Array(
                'name' => 'monitor',
                'attr' => Array(
                    'id' => 5235633
                ),
                'children' => Array(
                    Array(
                        'name' => 'url',
                        'attr' => Array(),
                        'children' => Array(
                            'http://www.bbc.co.uk/news'
                        )
                    )
                )
            )
        )
    )
)
);
array2xml($data);
?>