zoukankan      html  css  js  c++  java
  • SimpleXML读写XML文件以及json的读写

    SimpleXML写XML文件:

     1 <?php
     2 /**
     3 * function:SimpleXML写XML文件
     4 * author:JetWu
     5 * date:2016.12.03
     6 **/
     7 $mysqli = mysqli_connect('localhost', 'root', '123456', 'wjt');
     8 if(mysqli_connect_errno()) die('database connect fail:' . mysqli_connect_error());
     9  
    10 $sql = 'select * from study order by starttime';
    11 $res = mysqli_query($mysqli, $sql);
    12 $study = array();
    13 while($row = mysqli_fetch_array($res)) {
    14     $study[] = $row;
    15 }
    16  
    17 //XML标签配置
    18 $xmlTag = array(
    19     'starttime',
    20     'endtime',
    21     'school'
    22 );
    23 $xml = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><studentcareer />');
    24 foreach($study as $s) {
    25     $period = $xml->addChild('period');
    26     foreach($xmlTag as $x) {
    27         $period->addChild($x, $s[$x]);
    28     }
    29 }
    30 $xml->asXml('./write_sim.xml');//输出XML文件(没有格式化)

    SimpleXMLXML文件:

     1 <?php
     2 /**
     3 * function:SimpleXML读XML文件
     4 * author:JetWu
     5 * date:2016.12.03
     6 **/
     7 //XML标签配置
     8 $xmlTag = array(
     9     'starttime',
    10     'endtime',
    11     'school'
    12 );
    13 $study = array();
    14 $xml = simplexml_load_file('./write_sim.xml');
    15 foreach($xml->children() as $period) {
    16     $study[] = get_object_vars($period);//获取对象全部属性,返回数组
    17 }
    18 echo '<pre>';
    19 print_r($study);

    JSON的读写:

     1 // 追加写入用户名下文件
     2 $code="001";//动态数据
     3     $json_string = file_get_contents("text.json");// 从文件中读取数据到PHP变量
     4     $data = json_decode($json_string,true);// 把JSON字符串转成PHP数组
     5     $data[$code]=array("a"=>"as","b"=>"bs","c"=>"cs");
     6     $json_strings = json_encode($data);
     7     file_put_contents("text.json",$json_strings);//写入
     8 //修改
     9  $json_string = file_get_contents("text.json");// 从文件中读取数据到PHP变量
    10     $data = json_decode($json_string,true);// 把JSON字符串转成PHP数组
    11     $data["001"]["a"]="aas";
    12     $json_strings = json_encode($data);
    13     file_put_contents("text.json",$json_strings);//写入

    putjson.php:

     1 <?php
     2 // 生成一个PHP数组
     3 $data = array();
     4 $data[0] = array('1','吴者然','onestopweb.cn');
     5 $data[1] = array('2','何开','iteye.com');
     6 // 把PHP数组转成JSON字符串
     7 $json_string = json_encode($data);
     8 // 写入文件
     9 file_put_contents('test.json', $json_string);
    10 ?>

    getjson.php:

     1 <?php
     2 // 从文件中读取数据到PHP变量
     3 $json_string = file_get_contents('test.json');
     4 // 把JSON字符串转成PHP数组
     5 $data = json_decode($json_string, true);
     6 // 显示出来看看
     7 var_dump($data);
     8 echo '<br><br>';
     9 print_r($data);
    10 echo '<br><br>';
    11 echo '编号:'.$data[0][0].' 姓名:'.$data[0][1].' 网址:'.$data[0][2];
    12 echo '<br>';
    13 echo '编号:'.$data[1][0].' 姓名:'.$data[1][1].' 网址:'.$data[1][2];
    14 ?>
  • 相关阅读:
    常用PHP array数组函数
    每天学习30分钟新知识之html教程1
    laravel学习之路2: jwt集成
    JWT简介json web token bear token
    MDwiki 调研
    laravel学习之路1:认证相关
    OAuth 2.0介绍
    第一行代码 6.4 数据存储全方案-详解持久化数据- 数据库
    github(1)安装及使用图文详解
    Android集成讯飞语音、百度语音、阿里语音识别
  • 原文地址:https://www.cnblogs.com/mfBlog/p/10621557.html
Copyright © 2011-2022 走看看