php xml数据类型,PHP实现XML与数据格式进行转换类实例

2023-10-27

/**

* xml2array() will convert the given XML text to an array in the XML structure.

* Link: http://www.bin-co.com/php/scripts/xml2array/

* Arguments : $contents - The XML text

* $get_attributes - 1 or 0. If this is 1 the function will get the attributes as well as the tag values - this results in a different array structure in the return value.

* $priority - Can be 'tag' or 'attribute'. This will change the way the resulting array sturcture. For 'tag', the tags are given more importance.

* Return: The parsed XML in an array form. Use print_r() to see the resulting array structure.

* Examples: $array = xml2array(file_get_contents('feed.xml'));

* $array = xml2array(file_get_contents('feed.xml', 1, 'attribute'));

*/

function xml2array($contents, $get_attributes = 1, $priority = 'tag') {

if (!$contents) return array();

if (!function_exists('xml_parser_create')) {

// print "'xml_parser_create()' function not found!";

return array();

}

// Get the XML parser of PHP - PHP must have this module for the parser to work

$parser = xml_parser_create('');

xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, "UTF-8"); // http://minutillo.com/steve/weblog/2004/6/17/php-xml-and-character-encodings-a-tale-of-sadness-rage-and-data-loss

xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);

xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);

xml_parse_into_struct($parser, trim($contents), $xml_values);

xml_parser_free($parser);

if (!$xml_values) return; //Hmm...

// Initializations

$xml_array = array();

$parents = array();

$opened_tags = array();

$arr = array();

$current = &$xml_array; //Refference

// Go through the tags.

$repeated_tag_index = array(); //Multiple tags with same name will be turned into an array

foreach($xml_values as $data) {

unset($attributes, $value); //Remove existing values, or there will be trouble

// This command will extract these variables into the foreach scope

// tag(string), type(string), level(int), attributes(array).

extract($data); //We could use the array by itself, but this cooler.

$result = array();

$attributes_data = array();

if (isset($value)) {

if ($priority == 'tag') $result = $value;

else $result['value'] = $value; //Put the value in a assoc array if we are in the 'Attribute' mode

}

// Set the attributes too.

if (isset($attributes) and $get_attributes) {

foreach($attributes as $attr => $val) {

if ($priority == 'tag') $attributes_data[$attr] = $val;

else $result['attr'][$attr] = $val; //Set all the attributes in a array called 'attr'

}

}

// See tag status and do the needed.

if ($type == "open") { // The starting of the tag ''

$parent[$level-1] = &$current;

if (!is_array($current) or (!in_array($tag, array_keys($current)))) { // Insert New tag

$current[$tag] = $result;

if ($attributes_data) $current[$tag . '_attr'] = $attributes_data;

$repeated_tag_index[$tag . '_' . $level] = 1;

$current = &$current[$tag];

} else { // There was another element with the same tag name

if (isset($current[$tag][0])) { // If there is a 0th element it is already an array

$current[$tag][$repeated_tag_index[$tag . '_' . $level]] = $result;

$repeated_tag_index[$tag . '_' . $level]++;

} else { // This section will make the value an array if multiple tags with the same name appear together

$current[$tag] = array($current[$tag], $result); //This will combine the existing item and the new item together to make an array

$repeated_tag_index[$tag . '_' . $level] = 2;

if (isset($current[$tag . '_attr'])) { // The attribute of the last(0th) tag must be moved as well

$current[$tag]['0_attr'] = $current[$tag . '_attr'];

unset($current[$tag . '_attr']);

}

}

$last_item_index = $repeated_tag_index[$tag . '_' . $level]-1;

$current = &$current[$tag][$last_item_index];

}

} elseif ($type == "complete") { // Tags that ends in 1 line ''

// See if the key is already taken.

if (!isset($current[$tag])) { // New Key

$current[$tag] = $result;

$repeated_tag_index[$tag . '_' . $level] = 1;

if ($priority == 'tag' and $attributes_data) $current[$tag . '_attr'] = $attributes_data;

} else { // If taken, put all things inside a list(array)

if (isset($current[$tag][0]) and is_array($current[$tag])) { // If it is already an array...

// ...push the new element into that array.

$current[$tag][$repeated_tag_index[$tag . '_' . $level]] = $result;

if ($priority == 'tag' and $get_attributes and $attributes_data) {

$current[$tag][$repeated_tag_index[$tag . '_' . $level] . '_attr'] = $attributes_data;

}

$repeated_tag_index[$tag . '_' . $level]++;

} else { // If it is not an array...

$current[$tag] = array($current[$tag], $result); //...Make it an array using using the existing value and the new value

$repeated_tag_index[$tag . '_' . $level] = 1;

if ($priority == 'tag' and $get_attributes) {

if (isset($current[$tag . '_attr'])) { // The attribute of the last(0th) tag must be moved as well

$current[$tag]['0_attr'] = $current[$tag . '_attr'];

unset($current[$tag . '_attr']);

}

if ($attributes_data) {

$current[$tag][$repeated_tag_index[$tag . '_' . $level] . '_attr'] = $attributes_data;

}

}

$repeated_tag_index[$tag . '_' . $level]++; //0 and 1 index is already taken

}

}

} elseif ($type == 'close') { // End of tag ''

$current = &$parent[$level-1];

}

}

return($xml_array);

}

// Array to XML

class array2xml {

public $output = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";

public $sub_item = array();

public function __construct($array) {

$sub_item = array();

$this->output .= $this->xmlmake($array);

}

public function xmlmake($array, $fk = '') {

$xml = '';

global $sub_item;

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

if (is_array($value)) {

if (is_numeric($key)) {

$this->sub_item=array_merge($this->sub_item,array($fk));

$xml .= "" . $this->xmlmake($value, $key) . "{$fk}>";

} else {

$xml .= "" . $this->xmlmake($value, $key) . "{$key}>";

}

} else {

$xml .= "{$value}{$key}>\n";

}

}

return $xml;

}

public function output(){

foreach($this->sub_item as $t){

$this->output = str_replace("","",$this->output);

$this->output = str_replace("{$t}>{$t}>","{$t}>",$this->output);

}

return $this->output;

}

}

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

php xml数据类型,PHP实现XML与数据格式进行转换类实例 的相关文章

  • 【JS算法系列】1.翻转字符串的四种方法

    1 使用数组的 reverse 方法 let str hello let reversed str split reverse join olleh 2 从字符串的尾部开始迭代 并逐个添加字符到新的字符串中 let str hello le
  • qt打包应用程序

    cannot find gcc installation directory g exe must be in the path http t csdn cn d5yaf
  • 期权酱也来说说期权双卖策略原理

    期权option 又称选择权 是一类衍生品合约 买方支付权利金给卖方之后 买方有权在未来的特定日期或之前 以特定的价格向卖方买入或卖出标的资产的权利 下文介绍期权酱也来说说期权双卖策略原理 本文来自 期权酱 期权的本质简单说就是买方支付权利
  • 自己挖坑自己填,谷歌大改Transformer注意力,速度、内存利用率都提上去了

    点击上方 AI遇见机器学习 选择 星标 公众号 重磅干货 第一时间送达 来自 机器之心 考虑到 Transformer 对于机器学习最近一段时间的影响 这样一个研究就显得异常引人注目了 Transformer 有着巨大的内存和算力需求 因为
  • 挖掘机整体(2012.11.30)

    将近1周 终于把整体搞定了 这周的问题所在就是由于 180 ANGLE或者180 ANGLE的余弦值都相同 所以才会来回晃 我用了ASINF反正弦
  • Spring 框架学习—控制反转(IOC)

    Spring是一个开源框架 Spring是于2003 年兴起的一个轻量级的Java 开发框架 由Rod Johnson创建 简单来说 Spring是一个分层的JavaSE EEfull stack 一站式 轻量级开源框架 主要用于降低模块之
  • C#编程中的报错解决System.InvalidOperationException

    C 编程中的报错解决System InvalidOperationException 问题场景 问题原因 解决方案 方案一 打破规则 方案二 利用委托 Delegate 问题场景 在C Windows GUI编程中 从某个子线程去访问设计器
  • 计算机类毕业设计优秀最新题目

    主要有以下类别 python c语言 c vc opencv opengl程序设计 单片机类 51 stm32 arduino arm avr 龙芯开发板 嵌入式系统等硬件设计 程序设计 protues仿真 下列题目涵盖但不限于以上内容 4
  • 【Unity】渐入渐出的黑屏(白屏)过场动画最简单的实现

    一个简单的黑屏动画 用于2D项目 效果类似慢眨眼 进 出房间时切换场景的衔接过渡 这种效果的实现方式其实蛮多的 我能想到的包括用shader 用一张全黑Sprite覆盖屏幕 或者直接做成一个UI 最近学了一些UnityShader 所以首先
  • html title 属性多行显示

    html global attribute title displays in multiple lines 下面的代码段主要是展现了4种方式让 title 属性的多行展示 经测试 至少兼容到了 ie8
  • java JDBC

    JDBC是什么 Java DataBase Connectivity Java语言连接数据库 JDBC的本质是什么 一套接口 JDBC是SUN公司制定的一套接口 interface 接口都有调用者和实现者 面向接口调用 面向接口写实现类 这
  • Golang笔记:使用exec包执行外部程序与Shell命令

    文章目录 目的 使用演示 Cmd结构体 总结 目的 程序中执行外部程序是比较常用的功能 Golang执行外部程序可以使用标准库中的 os exec https pkg go dev os exec 这个包默认是用来执行外部程序的 可以通过调
  • 3 分钟掌握 Node.js 版本的区别

    在我们日常开发中 Node js 使用场景越来越多 大到服务端项目 小到开发工具脚本 所以掌握 Node js 一些基础知识是非常有必要的 今天主要聊一下 Node js 中 LTS 和 Current 的区别和如何选择合适的版本 一 版本
  • 前端在线预览word,excel,pdf

    前端在线预览word excel pdf 预览Word 预览pdf 预览Excel 预览Word 微软的在线预览功能 可以预览word ppt Excel PDF 局限 需要外网能访问文件 如果是只能内网用的系统就不适合了 XDOX预览 局
  • Linux组成员共享目录

    背景 在实际工作中常常会遇到这样的问题 一台服务器上的多个用户需要合作完成一个项目 他们都有自己的home目录和初始私有组 而他们需要同时再同一个目录下做开发 这时就会用到组成员共享目录 下面介绍如何让组成员再同一目录协同工作 以下操作默认
  • ./darknet: error while loading shared libraries: libcudart.so.8.0: cannot open shared object file:

    libcudart so 8 0 不能找到 是库文件路径引发的问题 可以到 etc ld so conf d目录下 修改其中任意一份conf文件 可以自建conf 以方便识别 将lib所在目录写进去 然后在终端输入 ldconfig 更新缓
  • Android客户端连接SSM(Spring+SpringMVC+Mybatis)框架Java服务器端

    Android客户端开发越来越流行 但是 Android客户端对应的服务器端采用什么框架呢 对于功能较为简单的应用 我建议 直接采用java web开发最简单的MVC框架即可 很多Android应用的服务器端开发框架 我都是采用这种 但是
  • Python安装及建立虚拟环境

    文章目录 前言 一 python安装 二 建立虚拟环境 三 安装jupyter notebook 总结 前言 Python由荷兰数学和计算机科学研究学会的吉多 范罗苏姆 于1990 年代初设计 作为一门叫做ABC语言的替代品 Python提
  • MongoDB 系统管理与操作详解

    一 MongoDB 启动与关闭 1 启动MongoDB 执行mongod命令即可启动MongoDB服务器 mongod在启动时可使用许多可配置选项 在命令行中运行mongod help可列出这些选项 下列选项十分常用 需着重注意 dbpat

随机推荐

  • JVM——垃圾回收

    垃圾回收 文章目录 垃圾回收 垃圾回收概述 什么是垃圾 为什么要回收垃圾 内存溢出和内存泄漏 java垃圾回收机制 自动内存管理 应该关心哪些区域的回收 垃圾回收相关算法 垃圾标记阶段算法 如何标记一个垃圾对象 什么样的对象能被标记为垃圾对
  • IntelliJ IDEA 高级调试技巧

    一 条件断点 循环中经常用到这个技巧 比如 遍历1个大List的过程中 想让断点停在某个特定值 参考上图 在断点的位置 右击断点旁边的小红点 会出来一个界面 在Condition这里填入断点条件即可 这样调试时 就会自动停在i 10的位置
  • WinPcap实战(一)——发送ARP包

    ARP包的结构 ARP包格式 物理帧头 14B ARP帧结构 28B 填充数据 18B CRC 4B 这里给出一张图 图中没有18字节的填充数据和4字节的校验位 物理帧头 14B 目的MAC 6B 源MAC 6B 类型 2B ARP帧 0x
  • 小程序通过webview实现本地任意文件上传

    微信小程序做文件上传的时候 只能选择相册的图片或者视频 没办法选择手机内存卡里的文件 比如 word pdf文件等等 下面可以通过 webview 的方式 借用 h5 的方式即可实现上面的功能 添加业务域名 webview里面打开的地址 首
  • Mybatis操作数据库--通用

    选中 xml 右键 Junite 测试的时候 Test位于这个包中 import org junit jupiter api Test 根据条件进行查询总记录数 条件如果涉及到两个表的内容 你也就只写一个表里面的 select count
  • 简易版python爬虫--通过关键字爬取网页

    背景 帮同学写了个爬虫程序 特此记录 怕以后忘了 这里是爬取百度https www baidu com 不为什么 主要就是百度老实 能爬 爬着简单 爬着不犯法 关键字爬取基本模板 import requests from bs4 impor
  • Flutter学习四:Flutter开发基础(五)资源管理

    目录 0 引言 1 资源管理 1 1 指定 assets 1 2 Asset 变体 variant 1 3 加载 assets 1 3 1 加载文本 1 3 2 加载图片 1 3 2 1 声明分辨率相关的图片 1 3 2 2 加载图片 1
  • SIFT特征提取分析

    SIFT Scale invariant feature transform 是一种检测局部特征的算法 该算法通过求一幅图中的特征点 interest points or corner points 及其有关scale 和 orientat
  • 音视频开发(13)---视频监控系统必须知道的一些基础知识

    视频监控系统必须知道的一些基础知识 前言 在视频监控系统中 视频文件的传输带宽很重要 那视频文件的传输带宽怎么计算呢 首先给大家介绍几个名词 正文 1 比特率 比特率是指每秒传送的比特 bit 数 单位为bps BitPerSecond 比
  • XSS靶场第三关秘籍

    查看页面源代码 直接插入我们的代码进行尝试 没有弹窗 查看页面源代码 发现我们的 被实体编码了 这里把 改成
  • DBUtils工具类的使用方法详解

    DBUtils使用方法详解 目录 DBUtils使用方法详解 一 前言 二 JDBC介绍 1 基本概念 2 JDBC访问数据库的流程 三 DBUtils介绍 1 基本概念 2 配置文件 3 创建JDBCUtils类 4 实现对数据表的增删改
  • springboot整合knife4j,从此告别手写接口文档

    关于knife4j Knife4j的前身是swagger bootstrap ui 前身swagger bootstrap ui是一个纯swagger ui的ui皮肤项目 一开始项目初衷是为了写一个增强版本的swagger的前端ui 但是随
  • 递归求和

    用阶乘求1到100的和 思路 递归结束条件 n 1 递归公式 n sum n 1 include
  • 泛型T E K V ?

    Java泛型中E T K V等的含义 Java泛型中的标记符含义 E Element 在集合中使用 因为集合中存放的是元素 T Type Java 类 K Key 键 V Value 值 N Number 数值类型 表示不确定的java类型
  • 蓝牙模块调试总结(BLE\HC-05\HC-06\HC-01\MLT-BT05)

    最近在使用蓝牙模块与手机进行联合调试 遇到了一些问题 在此总结一下 1 蓝牙模块分类 MLT BT05 4 0蓝牙模块 指令集详细说明 MLT BT05 4 0 蓝牙串口模块指令为 Command 指令集 注 发 AT 指令时必须回车换行
  • 针对三角形问题,使用边界值分析法设计测试用例

    一 测试问题描述 输入三个整数a b c 分别作为三角形的三条边 通过程序判断这三条边是否能构成三角形 如果能构成三角形 则判断三角形的类型 等边三角形 等腰三角形 一般三角形 要求输入三个整数a b c 必须满足以下条件 1 a 200
  • Satck与Queue要点

    Satck Stack定义 核心接口 stack源码 栈的实现 静态数组实现栈 动态数组实现栈 链表实现栈 queue queue与stack queue的实现 Satck Stack定义 stack的特点是先进先出 与queue正好相反
  • 一个局域网中,不同网段的服务器能通信吗?

    文章目录 起步 环境说明 B ping A A ping B 子网的误判 感谢 起步 在一个局域网中存在两台服务器 A B A IP 192 168 211 110 NETMASK 255 255 255 0 B IP 192 168 21
  • QT 新手小白USBCAN 学习

    一 介绍CAN总线 CAN总线介绍 二 USBCAN总线 2 1 产品介绍 USBCAN 转换器模块实现了将 CAN 总线接口与 USB 接口进行相互转换 可以 简单方便的通过电脑监视 CAN 总线网络 同时可以实现工业现场数据稳定的双 向
  • php xml数据类型,PHP实现XML与数据格式进行转换类实例

    xml2array will convert the given XML text to an array in the XML structure Link http www bin co com php scripts xml2arra