简体   繁体   English

PHP 数组到带有属性的 XML

[英]PHP array to XML with attributes

In CakePHP I know the XML-class in cake.libs.在 CakePHP 中,我知道 cake.libs 中的 XML 类。 What I like on this easy to use class is the fact that you can convert an array to xml with attributes.我喜欢这个易于使用的类的事实是,您可以将数组转换为带有属性的 xml。
On a current project I'm working with Zend Framework and I am missing this nice class.在当前的项目中,我正在使用 Zend Framework,但我错过了这个不错的课程。

Does anybody know how I can convert an PHP-array to XML with attributes easily?有谁知道如何轻松地将 PHP 数组转换为带有属性的 XML?

I tried this one but unfortunately its not easy to handle result arrays from database cause you have to define the attributes inside the array.我试过这个,但不幸的是它不容易处理来自数据库的结果数组,因为你必须在数组中定义属性。

Maybe I have missed something in ZF?也许我错过了采埃孚的一些东西? Or does anybody know how to adapt the CakePHP class to ZF?或者有人知道如何使 CakePHP 类适应 ZF?

Many thanks for any helpful hints.非常感谢任何有用的提示。

This function will allow you to create XML document with simple PHP array variable.此函数将允许您使用简单的 PHP 数组变量创建 XML 文档。 Nodes are just stacked onto each other and naming it 'Attribute_XXX' will add atribute XXX to that parent node.节点只是相互堆叠,将其命名为“Attribute_XXX”会将属性 XXX 添加到该父节点。

function to_xml(SimpleXMLElement $object, array $data)
{
    $attr = "Attribute_";
    foreach ($data as $key => $value) {
        if (is_array($value)) {
            $new_object = $object->addChild($key);
            to_xml($new_object, $value);
        } else {
            if(strpos($key, $attr) !== false){
                $object->addAttribute(substr($key, strlen($attr)), $value);
            }else{
                $object->addChild($key, $value);
            }
        }
    }
}

Usage:用法:

$my_array = array (
    'TagN1' => array (
        'TagInsideOfIt' => array (
            'Atrribute_IDuser' => 'anything',
            'RegularTag' => 'whatever',
            'Address' => array(
                'Attribute_ID' => '111',
                'Attribute_key' => 'aaa',
                'Company' => 'Google Inc.'
            )
        )
    )
);

$xml = new SimpleXMLElement('<root/>');

to_xml($xml, $my_array);

Header('Content-type: text/xml');
print($xml->asXML());

You can get some answers at this URL How to convert array to SimpleXML你可以在这个 URL How to convert array to SimpleXML得到一些答案

An alternative/fast and easy approach would be to convert array data into JSON, that would be faster as well using Zend library.另一种/快速且简单的方法是将数组数据转换为 JSON,使用 Zend 库也会更快。 Converting array into JSON is very easy as using follows functions.使用以下函数将数组转换为 JSON 非常容易。

Zend_Json::encode() and Zend_Json_Encoder::encode()

Tanks, a using the code of @jezda159 and i modified for me, this is my code: Tanks,使用@jezda159 的代码,我为我修改过,这是我的代码:

the array:数组:

   $elements_array = array(
            "node" => array(
                "subnode" => array(
                    "[attribute]" => "valueOfAttribute",
                    "[second-attribute]" => "valueOfAttribute",
                    "second-subnode" => array(
                        "[attribute]" => "valueOfAttribute",
                        "[second-attribute]" => "valueOfAttribute",
                    ),
                    "ListOfElements" => array(
                        "one",
                        "two",
                        "tree"
                    )
                )
            ),
        );

The code:代码:

    function array_to_xml($data, $object){
        foreach ($data as $key => $value) {
            $keyname = is_numeric( $key ) ? "item".$key : $key;
            if (is_array($value)) {
                $new_object = $object->addChild($keyname);
                array_to_xml($value, $new_object);
            } else {
                preg_match("#\[([a-z0-9-_]+)\]#i", $keyname, $attr);
                if( count($attr) ){
                    $object->addAttribute($attr[1], $value);
                }else{
                    $object->addChild($keyname, $value);
                }
            }
        }
    }
$xml_user_info = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8" ?><Elements></Elements>');

//function call to convert array to xml
array_to_xml($elements_array,$xml_user_info);
header('Content-type: text/xml');
//show generated xml file
echo $xml_user_info->asXML();

The result:结果:

<?xml version="1.0" encoding="UTF-8"?>
<Elements>
<node>
<subnode attribute="valueOfAttribute" second-attribute="valueOfAttribute">
<second-subnode attribute="valueOfAttribute" second-attribute="valueOfAttribute"/>
<ListOfElements>
<item0>one</item0>
<item1>two</item1>
<item2>tree</item2>
</ListOfElements>
</subnode>
</node>
</Elements>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM