php xml转为xml或者json
<?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的更多相关文章
- JS中将XML转为JSON对象
<script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.js"></script> <scr ...
- Python中xml、字典、json、类四种数据的转换
最近学python,觉得python很强很大很强大,写一个学习随笔,当作留念注:xml.字典.json.类四种数据的转换,从左到右依次转换,即xml要转换为类时,先将xml转换为字典,再将字典转换为j ...
- 都 2021 年了,竟然有人搞大数据时忽略 JSON 而去研究用 C# 把 XML 转换为 XML 的技术
在大数据项目开发过程中,ETL(Extract-Transform-Load)是必不可少.即便目前 JSON 非常流行,开发人员也有必定会有对远古系统的挑战,而 XML 格式的数据源作为经典存在浑身上 ...
- BIP_开发案例07_将原有Report Builer报表全部转为XML Publisher形式(案例)
2014-05-31 Created By BaoXinjian
- 使用php将数组转为XML
<?php class Array_to_Xml { private $version = '1.0'; private $encoding = 'UTF-8'; private $root = ...
- 【Flex】读取本地XML,然后XML数据转成JSON数据
干了一年H5,最近被要求写编辑器,Electron等级还不够,写不了,只有重新拿起as3,用flex,最近写到数据表编辑模块,有这部分功能,基本完成 . package utils { /** * 模 ...
- PHP. 03 .ajax传输XML、 ajax传输json、封装
XML简介 XML 指可扩展标记语言 EXtensible Markup Language .射击的时候是用来船体数据的,虽然格式跟HTML类似 xml示例 <?xml version=&quo ...
- json串转化成xml文件、xml文件转换成json串
1.json串转化成xml文件 p=[{"name":"tom","age":30,"sex":"男" ...
- 目标检测 的标注数据 .xml 转为 tfrecord 的格式用于 TensorFlow 训练
将目标检测 的标注数据 .xml 转为 tfrecord 的格式用于 TensorFlow 训练. import xml.etree.ElementTree as ET import numpy as ...
随机推荐
- 洛谷P2264 情书
P2264 情书 88通过 971提交 题目提供者lin_toto 标签字符串 难度提高+/省选- 提交该题 讨论 题解 记录 最新讨论 yyy快把题目改回来 噫 这题的题目好逗啊... 情书std ...
- Android基础总结(10)——手机多媒体的运用:通知、短信、相机、视频播放
Android提供了一系列的API,是我们可以在程序中调用很多手机的多媒体资源,从而编写出更加丰富的应用程序. 1.通知的使用 通知(Notification)是Android中比较有特色的一个功能, ...
- asp.net中导出Execl的方法
一.asp.net中导出Execl的方法: 在 asp.net中导出Execl有两种方法,一种是将导出的文件存放在服务器某个文件夹下面,然后将文件地址 输出在浏览器上:一种是将文件直接将文件输出流写给 ...
- 关于java classpath问题
在配置java环境的时候,有一个classpath,这是jre寻找.class文件的路径.一般会设置为当前路径".;%JAVA_HOME%\lib;",前面的.为当前路径 如果没有 ...
- 操作系统模仿CMD
实验一.命令解释程序的编写 专业:商软(2)班 姓名:列志华 学号:201406114254 一. 实验目的 (1)掌握命令解释程序的原理: (2)掌握简单的DOS调用方法: (3 ...
- Android WebRTC 音视频开发总结(四)-- webrtc传输模块
在介绍WebRTC通讯之前我们先来看一个P2P视频聊天包括的主要过程,转载请说明出处(博客园RTC.Blacker): 音视频数据采集->编码->发送->接收->解码-> ...
- 六、Struts2的配置文件
六.Struts2的配置文件 1.default.properties:在struts2-core-**.jar的org.apache.struts包中 关于Struts2一些常量配置(框架内部) s ...
- Java设计模式(Design Patterns In Java)读书摘要——第1章 绪论
为何需要模式 模式是做事的方法,是实现目标,研磨技术的方法.通俗点说,模式是为了解决某个行业的某个问题的有效的方法或技艺. 为何需要设计模式 为了提升代码的水准,是代码变得简洁而易用.模式是一种思想, ...
- js ajax乱码查看\u8fdb\u53e3
document.write('\u8fdb\u53e3') //在页面上看乱码转为中文 或在按F12 在console里查看 直接打'\u8fdb\u53e3'
- wpMVVM模式绑定集合的应用
一.新建一个项目,命名为wpMVVMone,添加一个有关食品信息的类Food.CS,代码如下: public class Food { public string Name { get; set; } ...