php xml转码,php xml 互相转换

//如果乱码的话更改header函数

class xmlarray

{

private $xml = ‘‘;//用于读取xml的变量

private $data;    //生成的xml

private $dom_tree;//xml目录树

/**

__construct仅用于读取xml

*/

function __construct($xml="")

{

if(empty($xml))

{

return null;

}

//$this->xml = $xml;

else

{

$this->loadxml($xml);

}

}

/**

装载要处理的xml文档也可以在初始化时装载

*/

public function loadxml($filepath)

{

$handle = @fopen($filepath,"r");

while (!feof($handle)&&$handle)

{

$xml_data .= fgets($handle, 4096);

}

$this->xml=$xml_data;

}

/**

处理xml文档

*/

private function _struct_to_array($values, &$i)

{

$child = array();

if (isset($values[$i][‘value‘])) array_push($child, $values[$i][‘value‘]);

while ($i++ < count($values))

{

switch ($values[$i][‘type‘])

{

case ‘cdata‘:

array_push($child, $values[$i][‘value‘]);

break;

case ‘complete‘:

$name = $values[$i][‘tag‘];

if(!empty($name))

{

$child[$name]= ($values[$i][‘value‘])?($values[$i][‘value‘]):‘‘;

if(isset($values[$i][‘attributes‘]))

{

$child[$name] = $values[$i][‘attributes‘];

}

}

break;

case ‘open‘:

$name = $values[$i][‘tag‘];

$size = isset($child[$name]) ? sizeof($child[$name]) : 0;

$child[$name][$size] = $this->_struct_to_array($values, $i);

break;

case ‘close‘:

return $child;

break;

}

}

return $child;

}

/**

处理xml文档,实际调用 _struct_to_array()处理

*/

public function xml2array()

{

$xml    =& $this->xml;

$values = array();

$index = array();

$array = array();

$parser = xml_parser_create();

xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);

xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);

xml_parse_into_struct($parser, $xml, $values, $index);

xml_parser_free($parser);

$i = 0;

$name = $values[$i][‘tag‘];

$array[$name] = isset($values[$i][‘attributes‘]) ? $values[$i][‘attributes‘] : ‘‘;

$array[$name] = $this->_struct_to_array($values, $i);

return $array;

}

//以下为将数组转换成xml的代码 使用dom

/**

读取数组

*/

public function array2xml($array){

if(!is_array($array)){

throw new Exception(‘array2xml requires an array‘, 1);

unset($array);

}

if(!count($array)){

throw new Exception(‘array is empty‘, 2);

unset($array);

}

$this->data = new DOMDocument();

$this->dom_tree = $this->data->createElement(‘result‘);//默认要标签

$this->data->appendChild($this->dom_tree);

$this->recurse_node($array, $this->dom_tree);

}

/**

处理数组为xml

*/

private function recurse_node($data, $obj){

$i = 0;

foreach($data as $key=>$value){

if(is_array($value)){

//recurse if neccisary

$sub_obj[$i] = $this->data->createElement($key);//创建标签

$obj->appendChild($sub_obj[$i]); //将标签加入到$obj标签下

$this->recurse_node($value, $sub_obj[$i]); //将值加入此标签下

} elseif(is_object($value)) {

//no object support so just say what it is

$sub_obj[$i] = $this->data->createElement($key, ‘Object: "‘ .

$key . ‘" type: "‘ . get_class($value) . ‘"‘);

$obj->appendChild($sub_obj[$i]);

} else {

//straight up data, no weirdness

$sub_obj[$i] = $this->data->createElement($key, $value);

//如果是最后的节点,将节点名和内容创建

$obj->appendChild($sub_obj[$i]);

}

$i++;

}

}

/**

将数组保存成xml文件

*/

public function saveXML($arraytoxml="")

{

if($arraytoxml=="")

{

$out = iconv("gbk","utf-8","请输入将要保存的文件名");

echo "";

}

else

{

return $this->data->save($arraytoxml);

}

}

}

?>

将数组转换成xml文件

$test = array(

‘one‘=>‘number‘,

‘two‘=> array(

‘c‘=>1,

‘d‘=>2,

‘e‘=>3,

‘f‘=>4

),

‘three‘=>‘alpha‘

);

/*创建对象*/

$xmlObj= new xmlarray();

/*转换*/

$xml = $xmlObj->array2xml($test);

/*输出保存*/

$xmlObj->saveXML("a.xml");

?>

将xml解析为数组输出<?php

/*创建对象*/

$xmlObj    = new xmlarray();

/*装载*/

$xml = $xmlObj->loadxml("a.xml");

/*转换*/

$data=$xmlObj->xml2array();

/*显示*/

print_r($data);

?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值