search
Homephp教程php手册php xml与json间的相互转换例子

xml转成json与json转成xml都是非常的简单了,但小编在网上找了N久没有找到,下面给各位整理一篇php xml与json间的相互转换例子,有兴趣的可进入看看.

今天在网上想找个将xml转成json的方法,找了半天没找到,找到了,根本没有什么用,有一个service_JSON说的挺像真的,找开后就一个JSON.php,php5.0以后已经把它加进来,json_decode和json_encode,想走捷径的,唉,靠人不如靠已,以下是我写的一个方法.

一,参考xml文件如下

<?xml version="1.0" encoding="UTF-8"   
  <humans>   
  <zhangying>   
  <name>张映</name>   
  <sex>男</sex>   
  <old>28</old>   
  </zhangying>   
  <tank>   
  <name>tank</name>   
  <sex>   
 <hao>yes</hao>   
 <aaaa>no</aaaa>   
 </sex>   
  <old>28</old>   
  </tank>   
  </humans>

二,xml转换成json

利用simplexml,代码如下:

public function xml_to_json($source) {   
	if(is_file($source)){             //传的是文件,还是xml的string的判断   
		$xml_array=simplexml_load_file($source);   
	}else{   
		$xml_array=simplexml_load_string($source);   
	}     
	$json = json_encode($xml_array);  //php5,以及以上,如果是更早版本,?下?JSON.php  
	return $json;   
}

三,json

转换成xml,利用递归函数,代码如下:

public function json_to_xml($source,$charset=&#39;utf8&#39;) {   
	if(emptyempty($source)){   
		return false;   
	}   
	$array = json_decode($source);  //php5,以及以上,如果是更早版本,?下?JSON.php   
	$xml  =&#39;<!--l version="1.0" encoding="&#39;.$charset.&#39;-->&#39;;   
	$xml .= $this->change($array);   
	return $xml;   
}   
   
public function change($source) {   
	$string="";  
	foreach($source as $k=>$v){  
		$string .="<".$k.">";  
		if(is_array($v) || is_object($v)){       //判断是否是数组,或者,对像  
			$string .= $this->change($v);        //是数组或者对像就的递归调用  
		}else{  
			$string .=$v;                        //取得标签数据  
		}  
		$string .="";   
	}   
	return $string;   
}

上面的方法json_to_xml,可以支持aaaa,不支持aaaaa看代码就能看明白.


教程网址:

欢迎收藏∩_∩但请保留本文链接。

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Atom editor mac version download

Atom editor mac version download

The most popular open source editor