php xml转记录集,php轻松实现xml转化成Array

php轻松实现xml转化成Array

$obj = simplexml_load_string($s);

$json = json_encode($obj);

print_r(json_decode($json, true));

强烈推荐

class XML {

protected $pointer;

public $degerler=array();

function loadString($string){

$this->pointer = simplexml_load_string($string);

return $this->pointer;

}

function loadFile($file){

$this->pointer = simplexml_load_file($file);

return $this->pointer;

}

function getname(){

return $this->pointer->getName();

}

function child(){

return $this->pointer->children();

}

function att(){

return $this->pointer->attributes();

}

function toArray(){

foreach ($this->child() as $sq){

$this->degerler[$this->getname()][$sq->getname()][] = $sq; // How many key

}

return $this->degerler;

}

}

------------------------------------

function xx($x,$p){

foreach ($x as $k=>$v){

if (is_object($v)) {

$x[$k] = xx((array)$v,$v);

}elseif (is_string($v) && $attr=$p->$k->attributes()){

if ($attr) {

$x[$k] = array('@attributes' => current((array)$attr),$v);

}

}

}

return $x;

}

$obj = simplexml_load_string($xml);

print_r(xx((array)$obj,$obj));

------------------------------------

不推荐使用, 学学php正则而已。

function xml2array($xml) {

$xmlary = array();

$reels = '/]*)\s*(?:\/>|>(.*))/s';

$reattrs = '/(\w+)=(?:"|\')([^"\']*)(:?"|\')/';

preg_match_all($reels, $xml, $elements);

foreach ($elements[1] as $ie => $xx) {

$xmlary[$ie]["name"] = $elements[1][$ie];

if ($attributes = trim($elements[2][$ie])) {

preg_match_all($reattrs, $attributes, $att);

foreach ($att[1] as $ia => $xx)

$xmlary[$ie]["attributes"][$att[1][$ia]] = $att[2][$ia];

}

$cdend = strpos($elements[3][$ie], " 0) {

$xmlary[$ie]["text"] = substr($elements[3][$ie], 0, $cdend - 1);

}

if (preg_match($reels, $elements[3][$ie]))

$xmlary[$ie]["elements"] = xml2array($elements[3][$ie]);

else if ($elements[3][$ie]) {

$xmlary[$ie]["text"] = $elements[3][$ie];

}

}

return $xmlary;

}

相关标签:php

本文原创发布php中文网,转载请注明出处,感谢您的尊重!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值