php中xml转json格式如何转换
这篇文章主要介绍php中xml转json格式如何转换,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!
创新互联公司专注于德兴企业网站建设,响应式网站,成都商城网站开发。德兴网站建设公司,为德兴等地区提供建站服务。全流程按需求定制开发,专业设计,全程项目跟踪,创新互联公司专业和态度为您提供的服务
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 输出: addtypes = false; // type="string|int|float|array|null|bool" public function export($data) { $data = array($this->root => $data); echo ''; $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,一个嵌套的缩写名称,是英文超级文本预处理语言(PHP:Hypertext Preprocessor)的缩写。PHP 是一种 HTML 内嵌式的语言,PHP与微软的ASP颇有几分相似,都是一种在服务器端执行的嵌入HTML文档的脚本语言,语言的风格有类似于C语言,现在被很多的网站编程人员广泛的运用。
以上是“php中xml转json格式如何转换”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注创新互联行业资讯频道!
文章名称:php中xml转json格式如何转换
文章出自:http://ybzwz.com/article/pigiid.html