新手求助高手解决 PHP数组转换XML问题,研究好久都不行.
<?php$team=array('id'=>'22955','video_order_id'=>'22955','is_audio'=>'0','status_form...
<?php
$team=array(
'id' => '22955',
'video_order_id' => '22955',
'is_audio' => '0',
'status_format' => '待审核',
'ftp_path' => '2013/1374/5695/2479/137456952479.ssm/',array('PreviewMTA'=>'A'),
array('PreviewMTA'=>'B'));
$xmlf = 'index2.xml';
function arrtoxml($arr,$flie,$dom=0,$item=0){
if (!$dom){
$dom = new DOMDocument("1.0","utf-8");
}
if(!$item){
$item = $dom->createElement("RegisterMMObjectRequest");
$dom->appendChild($item);
}
foreach ($arr as $key=>$val){
//$itemx = $dom->createElement(is_string($key)?$key:"PreviewMTA");
if(is_string($key)){ $itemx = $dom->createElement($key);
$item->appendChild($itemx);}
if (!is_array($val)){
$text = $dom->createTextNode($val);
$itemx->appendChild($text);
}
elseif (is_array($val)){
foreach ($val as $keys=>$vals)
{
$itemxx=$dom->createElement($keys);
$item->appendChild($itemxx);
if (!is_array($vals)){
$text = $dom->createTextNode($vals);
$itemxx->appendChild($text);
}
}
}
else {
arrtoxml($val,$dom,$itemx);
}
}
return $dom->save($flie);
}
arrtoxml($team,$xmlf);
?>
以上代码可以输出一个XML,但是我想有一段的XML可以下面这样
<PreviewsMTA>
<PreviewMTA>A</PreviewMTA>
<PreviewMTA>B</PreviewMTA>
。。。。。
</PreviewsMTA>
可惜我只能生成
<PreviewMTA>A</PreviewMTA>
<PreviewMTA>B</PreviewMTA>
请问怎么解决?
麻烦高手帮我更改下代码,谢谢了 展开
$team=array(
'id' => '22955',
'video_order_id' => '22955',
'is_audio' => '0',
'status_format' => '待审核',
'ftp_path' => '2013/1374/5695/2479/137456952479.ssm/',array('PreviewMTA'=>'A'),
array('PreviewMTA'=>'B'));
$xmlf = 'index2.xml';
function arrtoxml($arr,$flie,$dom=0,$item=0){
if (!$dom){
$dom = new DOMDocument("1.0","utf-8");
}
if(!$item){
$item = $dom->createElement("RegisterMMObjectRequest");
$dom->appendChild($item);
}
foreach ($arr as $key=>$val){
//$itemx = $dom->createElement(is_string($key)?$key:"PreviewMTA");
if(is_string($key)){ $itemx = $dom->createElement($key);
$item->appendChild($itemx);}
if (!is_array($val)){
$text = $dom->createTextNode($val);
$itemx->appendChild($text);
}
elseif (is_array($val)){
foreach ($val as $keys=>$vals)
{
$itemxx=$dom->createElement($keys);
$item->appendChild($itemxx);
if (!is_array($vals)){
$text = $dom->createTextNode($vals);
$itemxx->appendChild($text);
}
}
}
else {
arrtoxml($val,$dom,$itemx);
}
}
return $dom->save($flie);
}
arrtoxml($team,$xmlf);
?>
以上代码可以输出一个XML,但是我想有一段的XML可以下面这样
<PreviewsMTA>
<PreviewMTA>A</PreviewMTA>
<PreviewMTA>B</PreviewMTA>
。。。。。
</PreviewsMTA>
可惜我只能生成
<PreviewMTA>A</PreviewMTA>
<PreviewMTA>B</PreviewMTA>
请问怎么解决?
麻烦高手帮我更改下代码,谢谢了 展开
展开全部
<?php
$team=array(
'id' => '22955',
'video_order_id' => '22955',
'is_audio' => '0',
'status_format' => '待审核',
'ftp_path' => '2013/1374/5695/2479/137456952479.ssm/',
'lists' => array(array('PreviewMTA'=>'A','PreviewMTB'=>'B')),
'lists1' => array(array('PreviewMTA'=>'B'))
);
echo saveXML($team);
function saveXML($arr,$root = 'response'){
if(!preg_match('/[a-zA-Z][a-zA-Z0-9_]/',$root)){
$root = 'response';
}
$xml = new DOMDocument('1.0','UTF-8');
$xml->formatOutput = true;
$response = $xml->createElement($root);
$xml->appendChild($response);
foreach($arr as $key=>$value){
if(is_array($value)){
$first = $xml->createElement($key);
_createElement($value,$first,$xml);
}else{
$first = $xml->createElement($key,$value);
}
$response->appendChild($first);
}
return $xml->saveXML();
//$xml->save('create_xml.xml');
}
function _createElement($arr,$parentDom,&$xml){
foreach($arr as $value){
$list = $xml->createElement('list');
foreach($value as $k=>$val){
if(is_array($val)){
$dom = $xml->createElement($k);
_createElement($val,$dom, $xml);
}else{
$dom = $xml->createElement($k,$val);
}
$list->appendChild($dom);
}
$parentDom->appendChild($list);
}
}
?>
可能和你要的结果有偏离
追问
追答
function arrtoxml($arr,$dom=0,$item=0){
if (!$dom){
$dom = new DOMDocument("1.0");
}
if(!$item){
$item = $dom->createElement("root");
$dom->appendChild($item);
}
foreach ($arr as $key=>$val){
$itemx = $dom->createElement(is_string($key)?$key:"item");
$item->appendChild($itemx);
if (!is_array($val)){
$text = $dom->createTextNode($val);
$itemx->appendChild($text);
}else {
arrtoxml($val,$dom,$itemx);
}
}
return $dom->saveXML();
}
网上找的,这个应该是你要的效果
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询