<?php

class XmlToArray
{
private $xml;
private $contentAsName="content" ;
private $attributesAsName="attributes";
private $xml_array = array(); public function setXml( $xmlstr )
{
$this->xml = $xmlstr ;
return $this ;
} public function setContentAsName( $name )
{
$this->contentAsName = $name ;
return $this ;
} public function setAttributeAsName( $name )
{
$this->attributesAsName = $name ;
return $this ;
} private function createXMLArray( $node,&$parent_node,$node_index =0)
{
$node_attrbutes= array() ;
$node_name = $node->getName() ;
$attributes = $node->attributes() ;
$children = $node->children () ; // 遍历节点上的所有属性
foreach( $attributes as $attrname => $attrvalue )
{
$attrvalue = ( string )$attrvalue ;
$node_attrbutes[ $attrname ] = trim( $attrvalue ) ;
}
$content = "";
if( count($children) == 0 )
{
$content = ( string ) $node ;
} $node_array = array(
$this->attributesAsName =>$node_attrbutes ,
$this->contentAsName => trim( $content )
);
//设置层级关系
if( !isset( $parent_node[ $node_name ] ) )
{
$is = count( $parent_node ) ;
if( !isset( $parent_node[ $this->attributesAsName ] ) && count( $parent_node ) > 0 )
{ $last_index = count( $parent_node ) -1 ;
$parent_node =& $parent_node[ $last_index ];
$parent_node[ $node_name ] = $node_array ;
}
else
{
$parent_node[ $node_name ] = $node_array ;
}
}
else
{
$append = &$parent_node[ $node_name ] ;
if( isset( $append[ $this->attributesAsName ] ) )
{
$parent_node[ $node_name ] = array( $append );
$append = &$parent_node[ $node_name ] ; }
if( isset( $append[ $node_index ] ) )
{
$append = &$append[ $node_index ] ;
}
// 追加
array_push( $append , $node_array ) ;
} $index = 0 ;
// 递归操作
foreach( $children as $childnode )
{
$parent = &$parent_node[ $node_name ] ;
$this->createXMLArray( $childnode ,$parent,$index ++ );
}
return $parent_node ;
} public function parseXml( $isjson=false)
{
$root = simplexml_load_string ( $this->xml ) ;
$parent_node = array();
$array = $this->createXMLArray( $root ,$parent_node ) ; return $isjson ? json_encode( $array ) : $array ;
}
}
//解析为json
function toJSON()
{
require_once 'XmlToArray.php';
$XML= file_get_contents('simple.xml'); header("Content-type: text/html; charset=utf-8") ;
$xml_to_array = new XmlToArray();
$xml_to_array->setXml($XML);
// 当标签名与内置属性有冲突的时候可以自定义相关属性名,一般其概况不需要设置
//$xml_to_array->setAttributeAsName("attributeAsName")->setContentAsName("contentasName");
$r = $xml_to_array->parseXml(true);
print_r( $r ) ;
} // 解析为数组
function toArray()
{
require_once 'XmlToArray.php';
$XML = file_get_contents('simple.xml');
header("Content-type: text/html; charset=utf-8") ;
$xml_to_array = new XmlToArray();
$xml_to_array->setXml($XML);
// 当标签名与内置属性有冲突的时候可以自定义相关属性名,一般其概况不需要设置
//$xml_to_array->setAttributeAsName("attributeAsName")->setContentAsName("contentasName");
$r = $xml_to_array->parseXml();
print_r( $r ) ;
}

另外一种简单的方法,待验证

function xml2arr($xml){
$obj = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
$json = json_encode($obj);
$arr = json_decode($json, true);
return $arr;
}

转自:http://www.qaulau.com/php-xml-to-array-class/

http://www.thinkphp.cn/code/1123.html

php xml转为xml或者json的更多相关文章

  1. JS中将XML转为JSON对象

    <script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.js"></script> <scr ...

  2. Python中xml、字典、json、类四种数据的转换

    最近学python,觉得python很强很大很强大,写一个学习随笔,当作留念注:xml.字典.json.类四种数据的转换,从左到右依次转换,即xml要转换为类时,先将xml转换为字典,再将字典转换为j ...

  3. 都 2021 年了,竟然有人搞大数据时忽略 JSON 而去研究用 C# 把 XML 转换为 XML 的技术

    在大数据项目开发过程中,ETL(Extract-Transform-Load)是必不可少.即便目前 JSON 非常流行,开发人员也有必定会有对远古系统的挑战,而 XML 格式的数据源作为经典存在浑身上 ...

  4. BIP_开发案例07_将原有Report Builer报表全部转为XML Publisher形式(案例)

    2014-05-31 Created By BaoXinjian

  5. 使用php将数组转为XML

    <?php class Array_to_Xml { private $version = '1.0'; private $encoding = 'UTF-8'; private $root = ...

  6. 【Flex】读取本地XML,然后XML数据转成JSON数据

    干了一年H5,最近被要求写编辑器,Electron等级还不够,写不了,只有重新拿起as3,用flex,最近写到数据表编辑模块,有这部分功能,基本完成 . package utils { /** * 模 ...

  7. PHP. 03 .ajax传输XML、 ajax传输json、封装

    XML简介 XML 指可扩展标记语言 EXtensible Markup Language .射击的时候是用来船体数据的,虽然格式跟HTML类似 xml示例 <?xml version=&quo ...

  8. json串转化成xml文件、xml文件转换成json串

    1.json串转化成xml文件 p=[{"name":"tom","age":30,"sex":"男" ...

  9. 目标检测 的标注数据 .xml 转为 tfrecord 的格式用于 TensorFlow 训练

    将目标检测 的标注数据 .xml 转为 tfrecord 的格式用于 TensorFlow 训练. import xml.etree.ElementTree as ET import numpy as ...

随机推荐

  1. 洛谷P2737 [USACO4.1]麦香牛块Beef McNuggets

    P2737 [USACO4.1]麦香牛块Beef McNuggets 13通过 21提交 题目提供者该用户不存在 标签USACO 难度普及+/提高 提交  讨论  题解 最新讨论 暂时没有讨论 题目描 ...

  2. java中反射

    Person.java===>>person.class ==>>jvm中的类加载器===>>class对象:代表内存中Person.class ==>> ...

  3. ASP.NET MVC4 学习系统一(项目模板)

    项目模板 1.空模板      空模板用于创建ASP.NETMVC 4网站的架构,包含基本的文件夹结构,以及需要引用的asp.netmvc程序集,也包含可能要使用的javaScript 库.模板同样包 ...

  4. Android控件大全(二)——Toolbar

    1.隐藏Actionbar 代码中设置:requestWindowFeature(Window.FEATURE_NO_TITLE)     //如果Activity是继承自AppCompatActiv ...

  5. meta基础知识

    H5页面窗口自动调整到设备宽度,并禁止用户缩放页面 <meta name="viewport" content="width=device-width,initia ...

  6. ASP.NET的SEO:使用.ashx文件——排除重复内容

    本系列目录 不同的链接指向的页面如果具有大量相同的内容,这种现象就会被称为"重复内容",如果一个网站的重复内容很多,搜索引擎就会认为这个网站的价值不高.所以我们应尽量避免各种重复内 ...

  7. 三、第一个Struts2应用案例(编码步骤)

    第一个Struts2应用案例(编码步骤) 编写2个jsp hello.jsp <body>     <a href="${pageContext.request.conte ...

  8. swift皮筋弹动发射飞机

    今天在那个ios教程网上看到了一个不错的ios游戏源码,这是一个款采用swift实现的皮筋弹动发射飞机游戏源码,游戏源码比较详细,大家可以研究学习一下吧. <ignore_js_op> & ...

  9. mysql快速上手1

    mysql简介 1.什么是数据库 ? 数据库(Database)是按照数据结构来组织.存储和管理数据的仓库,它产生于距今六十多年前,随着信息技术和市场的发展,特别是二十世纪九十年代以后,数据管理不再仅 ...

  10. 将List<T>转化成 DataTable--调整可空类型的转化错误

    加载表结构并保持成XML string cmdText = @"select * from kb_lable_temp where 1=2"; using (SqlConnecti ...