php xml转json格式的方法:首先创建一个php示例文件;然后通过“function xmltoarray($xml, $options = array()){...}”方法将一个xml文件的数据转换为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);json 转 xml以下代码将 json 数据格式作为 xml 输出:<?php$json = stream_get_contents(stdin);$data = @json_decode($json, false);if (!is_array($data) && !is_object($data)) { echo 'error: invalid json given' . php_eol; exit(1);}class exporter{ private $root = 'document'; private $indentation = ' '; // todo: private $this->addtypes = false; // type="string|int|float|array|null|bool" public function export($data) { $data = array($this->root => $data); echo '<?xml version="1.0" encoding="utf-8">'; $this->recurse($data, 0); echo php_eol; } private function recurse($data, $level) { $indent = str_repeat($this->indentation, $level); foreach ($data as $key => $value) { echo php_eol . $indent . '<' . $key; if ($value === null) { echo ' />'; } else { echo '>'; if (is_array($value)) { if ($value) { $temporary = $this->getarrayname($key); foreach ($value as $entry) { $this->recurse(array($temporary => $entry), $level + 1); } echo php_eol . $indent; } } else if (is_object($value)) { if ($value) { $this->recurse($value, $level + 1); echo php_eol . $indent; } } else { if (is_bool($value)) { $value = $value ? 'true' : 'false'; } echo $this->escape($value); } echo '</' . $key . '>'; } } } private function escape($value) { // todo: return $value; } private function getarrayname($parentname) { // todo: special namding for tag names within arrays return $parentname; }}$exporter = new exporter();$exporter->export($data);
推荐学习:《php视频教程》
以上就是php xml转json格式怎么转换的详细内容。