将PHP关联数组与XML相互传递 [英] Passing PHP associative arrays to and from XML

查看:72
本文介绍了将PHP关联数组与XML相互传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种简单的方法可以将PHP关联数组与XML进行封送处理?例如,我有以下数组:

Is there an easy way to marshal a PHP associative array to and from XML? For example, I have the following array:

$items = array("1", "2",
    array(
        "item3.1" => "3.1",
        "item3.2" => "3.2"
        "isawesome" => true
    )
);

如何在尽可能少的行中将其转换为类似于以下XML的内容,然后再次返回?

How would I turn it into something similar to the following XML in as few lines as possible, then back again?

<items>
    <item>1</item>
    <item>2</item>
    <item>
        <item3_1>3.1</item3_1>
        <item3_2>3.2</item3_2>
        <isawesome>true</isawesome>
    </item>
</items>

我不太在乎是否需要稍微更改数组结构,或者输出的XML是否与上面的示例不同.我一直在尝试使用PHP的 XMLReader

I don't really care if I have to change the array structure a bit or if the XML that comes out is different to the above example. I've been trying to work with PHP's XMLReader and XMLWriter, but the documentation is so poor and the code I've produced as a consequence looks nothing like what I feel it should look like:

$xml = SomeXMLWriter::writeArrayToXml($items);
$array = SomeXMLWriter::writeXmlToArray($xml);

在没有编写自己的自定义类的情况下,获取PHP数组的基本原始XML转储真的要比这难吗?

Does it really have to be any harder than that to get a basic, raw XML dump of a PHP array without writing my own custom class?

我尽量避免吃梨.除了配置上令人头疼的问题之外,我从未使用过使用过的任何软件包.

I try to avoid PEAR. In addition to the configuration headaches I've had with it, I've never stuck with any of the packages I've ever used from it.

推荐答案

对于那些不使用PEAR软件包但已安装PHP5的用户.这对我有用:

For those of you not using the PEAR packages, but you've got PHP5 installed. This worked for me:

/**
 * Build A XML Data Set
 *
 * @param array $data Associative Array containing values to be parsed into an XML Data Set(s)
 * @param string $startElement Root Opening Tag, default fx_request
 * @param string $xml_version XML Version, default 1.0
 * @param string $xml_encoding XML Encoding, default UTF-8
 * @return string XML String containig values
 * @return mixed Boolean false on failure, string XML result on success
 */
public function buildXMLData($data, $startElement = 'fx_request', $xml_version = '1.0', $xml_encoding = 'UTF-8') {
    if(!is_array($data)) {
        $err = 'Invalid variable type supplied, expected array not found on line '.__LINE__." in Class: ".__CLASS__." Method: ".__METHOD__;
        trigger_error($err);
        if($this->_debug) echo $err;
        return false; //return false error occurred
    }
    $xml = new XmlWriter();
    $xml->openMemory();
    $xml->startDocument($xml_version, $xml_encoding);
    $xml->startElement($startElement);

    /**
     * Write XML as per Associative Array
     * @param object $xml XMLWriter Object
     * @param array $data Associative Data Array
     */
     function write(XMLWriter $xml, $data) {
         foreach($data as $key => $value) {
             if(is_array($value)) {
                 $xml->startElement($key);
                 write($xml, $value);
                 $xml->endElement();
                 continue;
             }
             $xml->writeElement($key, $value);
         }
     }
     write($xml, $data);

     $xml->endElement();//write end element
     //Return the XML results
     return $xml->outputMemory(true); 
}

这篇关于将PHP关联数组与XML相互传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆