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 ...
随机推荐
- iOS 如何根据经纬度来定位位置
/** * 初始化一个经纬度结构体 */ CLLocationCoordinate2D center = {[latitudeTF.text floatValue],[longitudeTF.tex ...
- 线程中CreateEvent和SetEvent及WaitForSingleObject的用法
首先介绍CreateEvent是创建windows事件的意思,作用主要用在判断线程退出,线程锁定方面. CreateEvent 函功能描述:创建或打开一个命名的或无名的事件对象. EVENT有两种状态 ...
- PAT1007
#include<stdio.h>#include<vector>#include<algorithm>using namespace std; int main( ...
- C# 加密算法
public static class Common { #region MD5加密 /// <summary> /// M ...
- 响应式Web设计基础
本文所有内容来自Responsive Web Design Fundamentals 手机.大屏手机.平板电脑.桌面电脑.游戏控制台.电视.甚至是可穿戴设备,如此多的设备也形成了多种多样的屏幕尺寸.屏 ...
- Spring框架搭建遇到的问题汇总
1.The resource is not on the build path of a Java project 然后把相应的依赖加入构建路径 2.Type mismatch: cannot con ...
- Html.ActionLink
一 Html.ActionLink("linkText","actionName") 该重载的第一个参数是该链接要显示的文字,第二个参数是对应的控制器的方法, ...
- Windows Phone 显示长文本
文采不好,将就着看,见谅 思路最重要,故本文不提供源码下载 参考项目: http://www.windowsphone.com/zh-cn/store/app/%E5%BF%83%E7%90%86fm ...
- 计算2的n次方的三种方法(C语言实现)
C代码如下: #include <stdio.h> int func1(int n) { <<n; } int func2(int n) { ) { ; } )*; } int ...
- excel上传和下载
需要注意的地方: 1.js构造表单并提交 2.js中文传参encodeURI(encodeURI("中文")),action接收并转换value = URLDecoder.deco ...