首页 > php开发 > PHP XML数据解析代码,json,parser函数

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

<?php
//xml string  
$xml_string="<?xml version='1.0'  
<users>  
<user id='398'>  
<name>Foo</name>  
<email>foo@bar.com</name>  
</user>  
<user id='867'>  
<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['id'], ' ';  
//subnodes are accessed by -> operator  
echo $user->name, ' ';  
echo $user->email, '<br />';  
} 
json数据解析代码如下:
$json_string='{"id":1,"name":"foo","email":"foo@foobar.com","interest":["wordpress","php"]} '; 
$obj=json_decode($json_string); 
echo $obj->name; //prints foo 
echo $obj->interest[1]; //prints php
//xml string 
$xml_string="<?xml version='1.0' 
<users> 
<user id='398'> 
<name>Foo</name> 
<email>foo@bar.com</name> 
</user> 
<user id='867'> 
<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['id'], ' '; 
//subnodes are accessed by -> operator 
echo $user->name, ' '; 
echo $user->email, '<br />'; 
}
?>

php还自带了一个PHP XML Parser

PHP XML Parser 简介

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


本文地址:http://www.phprm.com/develop/fs4563.html

转载随意,但请附上文章地址:-)

标签:php xml数据解析 json函数

相关文章

发表留言