php 怎么将数组转xml的函数
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php 怎么将数组转xml的函数相关的知识,希望对你有一定的参考价值。
没有现成函数,只能自己写;我有一个别人写的函数:<?php
class A2Xml
private $version = '1.0';
private $encoding = 'UTF-8';
private $root = 'root';
private $xml = null;
function __construct()
$this->xml = new XmlWriter();
function toXml($data, $eIsArray=FALSE)
if(!$eIsArray)
$this->xml->openMemory();
$this->xml->startDocument($this->version, $this->encoding);
$this->xml->startElement($this->root);
foreach($data as $key => $value)
if(is_array($value))
$this->xml->startElement($key);
$this->toXml($value, TRUE);
$this->xml->endElement();
continue;
$this->xml->writeElement($key, $value);
if(!$eIsArray)
$this->xml->endElement();
return $this->xml->outputMemory(true);
$res = array(
'hello' => '11212',
'world' => '232323',
'array' => array(
'test' => 'test',
'b' => array('c'=>'c', 'd'=>'d')
),
'a' => 'haha'
);
$xml = new A2Xml();
echo $xml->toXml($res); 参考技术A php没有现成的函数,只能自己遍历数组再去拼xml 参考技术B <?php
class A2Xml
private $version = '1.0';
private $encoding = 'UTF-8';
private $root = 'root';
private $xml = null;
function __construct()
$this->xml = new XmlWriter();
function toXml($data, $eIsArray=FALSE)
if(!$eIsArray)
$this->xml->openMemory();
$this->xml->startDocument($this->version, $this->encoding);
$this->xml->startElement($this->root);
foreach($data as $key => $value)
if(is_array($value))
$this->xml->startElement($key);
$this->toXml($value, TRUE);
$this->xml->endElement();
continue;
$this->xml->writeElement($key, $value);
if(!$eIsArray)
$this->xml->endElement();
return $this->xml->outputMemory(true);
$res = array(
'hello' => '11212',
'world' => '232323',
'array' => array(
'test' => 'test',
'b' => array('c'=>'c', 'd'=>'d')
),
'a' => 'haha'
);
$xml = new A2Xml();
echo $xml->toXml($res); 参考技术C
这里有个封装函数,不知道能不能帮到
以上是关于php 怎么将数组转xml的函数的主要内容,如果未能解决你的问题,请参考以下文章