Rumah > php教程 > php手册 > teks badan

PHP XML数据解析代码,json,parser函数

WBOY
Lepaskan: 2016-05-25 16:41:56
asal
1089 orang telah melayarinya

<?php
//xml string  
$xml_string="<?xml version=&#39;1.0&#39;  
<users>  
<user id=&#39;398&#39;>  
<name>Foo</name>  
<email>foo@bar.com</name>  
</user>  
<user id=&#39;867&#39;>  
<name>Foobar</name>  
<email>foobar@foo.com</name>  
</user>  
</users>"; 
 
//load the xml string using simplexml  
$xml = simplexml_load_string($xml_string); 
 
//loop through the each node of user  
foreach ($xml->user as $user)  
{  
//access attribute  
echo $user[&#39;id&#39;], &#39; &#39;;  
//subnodes are accessed by -> operator  
echo $user->name, &#39; &#39;;  
echo $user->email, &#39;<br />&#39;;  
} 
json数据解析代码如下:
$json_string=&#39;{"id":1,"name":"foo","email":"foo@foobar.com","interest":["wordpress","php"]} &#39;; 
$obj=json_decode($json_string); 
echo $obj->name; //prints foo 
echo $obj->interest[1]; //prints php
//xml string 
$xml_string="<?xml version=&#39;1.0&#39; 
<users> 
<user id=&#39;398&#39;> 
<name>Foo</name> 
<email>foo@bar.com</name> 
</user> 
<user id=&#39;867&#39;> 
<name>Foobar</name> 
<email>foobar@foo.com</name> 
</user> 
</users>"; 
 
//load the xml string using simplexml 
$xml = simplexml_load_string($xml_string); 
 
//loop through the each node of user 
foreach ($xml->user as $user) 
{
//access attribute 
echo $user[&#39;id&#39;], &#39; &#39;; 
//subnodes are accessed by -> operator 
echo $user->name, &#39; &#39;; 
echo $user->email, &#39;<br />&#39;; 
}
?>
Salin selepas log masuk

php还自带了一个PHP XML Parser

PHP XML Parser 简介

XML 函数允许我们解析 XML 文档,但无法对其进行验证,XML 是一种用于标准结构化文档交换的数据格式.


Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Cadangan popular
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan