php xml怎么转化成json
时间:2021-09-08
来源:互联网
标签:
今天PHP爱好者给大家带来php xml转化成json的方法教程:1、创建一个PHP示例文件;2、找到需要转换的xml文件;3、通过“function xmlToArray($xml, $options = array()) {...}”方法将其转换成json即可。希望对大家有所帮助。
本文操作环境:Windows7系统、PHP7.1版本、Dell G3电脑
php xml怎么转化成json?
XML 转 JSON
以下代码演示了如何将一个 xml 文件的数据转换为 Json 格式数据:
function xmlToArray($xml, $options = array()) {
$defaults = array(
'namespaceSeparator' => ':',//you may want this to be something other than a colon
'attributePrefix' => '@', //to distinguish between attributes and nodes with the same name
'alwaysArray' => array(), //array of xml tag names which should always become arrays
'autoArray' => true, //only create arrays for tags which appear more than once
'textContent' => '$', //key used for the text content of elements
'autoText' => true, //skip textContent key if node has no attributes or child nodes
'keySearch' => false, //optional search and replace on tag and attribute names
'keyReplace' => false //replace values for above search values (as passed to str_replace())
);
$options = array_merge($defaults, $options);
$namespaces = $xml->getDocNamespaces();
$namespaces[''] = null; //add base (empty) namespace
//get attributes from all namespaces
$attributesArray = array();
foreach ($namespaces as $prefix => $namespace) {
foreach ($xml->attributes($namespace) as $attributeName => $attribute) {
//replace characters in attribute name
if ($options['keySearch']) $attributeName =
str_replace($options['keySearch'], $options['keyReplace'], $attributeName);
$attributeKey = $options['attributePrefix']
. ($prefix ? $prefix . $options['namespaceSeparator'] : '')
. $attributeName;
$attributesArray[$attributeKey] = (string)$attribute;
}
}
//get child nodes from all namespaces
$tagsArray = array();
foreach ($namespaces as $prefix => $namespace) {
foreach ($xml->children($namespace) as $childXml) {
//recurse into child nodes
$childArray = xmlToArray($childXml, $options);
list($childTagName, $childProperties) = each($childArray);
//replace characters in tag name
if ($options['keySearch']) $childTagName =
str_replace($options['keySearch'], $options['keyReplace'], $childTagName);
//add namespace prefix, if any
if ($prefix) $childTagName = $prefix . $options['namespaceSeparator'] . $childTagName;
if (!isset($tagsArray[$childTagName])) {
//only entry with this key
//test if tags of this type should always be arrays, no matter the element count
$tagsArray[$childTagName] =
in_array($childTagName, $options['alwaysArray']) || !$options['autoArray']
? array($childProperties) : $childProperties;
} elseif (
is_array($tagsArray[$childTagName]) && array_keys($tagsArray[$childTagName])
=== range(0, count($tagsArray[$childTagName]) - 1)
) {
//key already exists and is integer indexed array
$tagsArray[$childTagName][] = $childProperties;
} else {
//key exists so convert to integer indexed array with previous value in position 0
$tagsArray[$childTagName] = array($tagsArray[$childTagName], $childProperties);
}
}
}
//get text content of node
$textContentArray = array();
$plainText = trim((string)$xml);
if ($plainText !== '') $textContentArray[$options['textContent']] = $plainText;
//stick it all together
$propertiesArray = !$options['autoText'] || $attributesArray || $tagsArray || ($plainText === '')
? array_merge($attributesArray, $tagsArray, $textContentArray) : $plainText;
//return node as array
return array(
$xml->getName() => $propertiesArray
);
}
使用实例
$xmlNode = simplexml_load_file('example.xml');
$arrayData = xmlToArray($xmlNode);
echo json_encode($arrayData);
以上就是php xml怎么转化成json的详细内容,更多请关注php爱好者其它相关文章!
-
如何注册谷歌账号(谷歌账号注册方法) 怎么跳过手机验证 时间:2025-09-29
-
access数据库8个经典实例 时间:2025-09-29
-
mmc.exe是什么进程 mmc.exe应用程序错误的原因及解决方法 时间:2025-09-29
-
4种基本的编程命名规范介绍(匈牙利命名法、驼峰式命名法、帕斯卡命名法、下划线命名法) 时间:2025-09-29
-
Ghostscript下载、安装教程 Ghostscript命令参数详解 时间:2025-09-29
-
Linux中内存管理NUMA架构详解 时间:2025-09-29
今日更新
-
顶级弓箭手零氪怎么玩-零氪玩法详细
阅读:18
-
为了吾王单人最强职业是什么-单人职业推荐
阅读:18
-
二重螺旋魔之楔怎么选-二重螺旋六种魔之楔效果详解
阅读:18
-
三国群英传策定九州新手阵容怎么搭配
阅读:18
-
伊瑟惠特妮怎么打-伊瑟惠特妮通关打法详解
阅读:18
-
币安APP官方下载:安全合规的虚拟币交易平台首选
阅读:18
-
辉烬赤痕阵容怎么搭配-辉烬赤痕配队方法
阅读:18
-
机械启元机械兽有哪些-机械启元机械兽
阅读:18
-
孙策梗是什么梗揭秘三国猛男搞笑名场面 看完秒懂江东小霸王为何爆火网络
阅读:18
-
洛克王国世界三测时间-洛克王国新作内测开启时间预测
阅读:18