1. 程式人生 > >php操作XML,讀取資料和寫入資料的方法

php操作XML,讀取資料和寫入資料的方法

xml檔案

<?xml version="1.0" encoding="utf-8"?>
	
<vip>

	<id>23</id>
		
	<username>開心的路飛</username>
		
	<sex>男</sex>
		
	<face>face/43.jpg</face>
		
	<email>[email protected]</email>
		
	<qq>1212121212</qq>
	
</vip>
php解析XML獲取標籤中的值
/*
 * _get_xml 獲取的XML檔案
* @access public 表示函式對外公開
* @param $_xmlfile xml檔案
* $_html 從XML中取出的資料陣列
* */
function _get_xml($_xmlfile){
    $_html = array();
    if(file_exists($_xmlfile)){
        $_xml = file_get_contents($_xmlfile);
        preg_match_all('/<vip>(.*)<\/vip>/', $_xml,$_dom);       
        foreach($_dom[1] as $_value){
            preg_match_all('/<id>(.*)<\/id>/', $_value,$_id);
            preg_match_all('/<username>(.*)<\/username>/', $_value,$_username);
            preg_match_all('/<sex>(.*)<\/sex>/', $_value,$_sex);
            preg_match_all('/<face>(.*)<\/face>/', $_value,$_face);
            preg_match_all('/<email>(.*)<\/email>/', $_value,$_email);
            preg_match_all('/<qq>(.*)<\/qq>/', $_value,$_qq);
            $_html['id'] = $_id[1][0];
            $_html['username'] = $_username[1][0];
            $_html['sex'] = $_sex[1][0];
            $_html['face'] = $_face[1][0];
            $_html['email'] = $_email[1][0];
            $_html['qq'] = $_qq[1][0];
        }
    }else{
       _alert_back("檔案不存在");
    }
    return  $_html;
}
php向XML檔案中寫入資料
/*
 * _set_xml將資訊寫入XML檔案
* @access public 表示函式對外公開
* @param $_xmlfile xml檔案
* @param $_clean 要寫入的資訊的陣列
* */
function _set_xml($_xmlfile,$_clean){
    $_fp = @fopen('newuser.xml','w');
    if(!$_fp){
        exit('系統錯誤,檔案不存在!');
    }
    flock($_fp,LOCK_EX);
    $_string = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\t";
    fwrite($_fp, $_string,strlen($_string));
    $_string = "<vip>\r\t";
    fwrite($_fp, $_string,strlen($_string));
    $_string = "\t<id>{$_clean['id']}</id>\r\t";
    fwrite($_fp, $_string,strlen($_string));
    $_string = "\t<username>{$_clean['username']}</username>\r\t";
    fwrite($_fp, $_string,strlen($_string));
    $_string = "\t<sex>{$_clean['sex']}</sex>\r\t";
    fwrite($_fp, $_string,strlen($_string));
    $_string = "\t<face>{$_clean['face']}</face>\r\t";
    fwrite($_fp, $_string,strlen($_string));
    $_string = "\t<email>{$_clean['email']}</email>\r\t";
    fwrite($_fp, $_string,strlen($_string));
    $_string = "\t<qq>{$_clean['url']}</qq>\r\t";
    fwrite($_fp, $_string,strlen($_string));
    $_string = "</vip>";
    fwrite($_fp, $_string,strlen($_string));
    flock($_fp,LOCK_UN);
    fclose($_fp);
}




相關推薦

php操作XML讀取資料寫入資料方法

xml檔案 <?xml version="1.0" encoding="utf-8"?> <vip> <id>23</id> <username>開心的路飛</username>

Android使用上下文獲取資料寫入資料

本文是在上一篇博文的基礎上使用上下文儲存使用者名稱和密碼,讀取使用者名稱和密碼的 上一篇博文連結:https://blog.csdn.net/liyunfu233/article/details/84072958 下面是和上一篇博文不同之處,因為修改了儲存資料和讀取資料的形參,所以在Main

POI操作Excel詳解讀取xlsxlsx格式的文件

shee xss split 類型 後綴 .sh lan xls lin package org.ian.webutil; import java.io.File; import java.io.FileInputStream; import java.io.FileN

mongo-spark-讀取不同的庫資料寫入不同的庫中

mongo-spark-讀取不同的庫資料和寫入不同的庫中 package com.example.app import com.mongodb.spark.config.{ReadConfig, WriteConfig} import com.mongodb.spark.sql._ object

C#讀寫註冊列表(寫入註冊列表讀取註冊列表的資料

1.建立winfrom專案 ,在load事件中寫方法(只看load事件裡的程式碼就可以) ps:如需要專案的原始碼,可去我的資源中下載 from1窗體程式碼 using Microsoft.Win32; using System; using System.Collections.Generi

SpringBoot利用java反射機制實現靈活讀取Excel表格中的資料匯出資料至Excel表格

如果直接把固定Excel表格模板中的資料匯入資料庫,直接將Excel中的資料讀取出來,再存入資料庫中即可,將資料匯出至固定的Excel模板方法類似。但是,有時候,開發製作的模板與客戶需要的往往有些許差別(欄位排版、欄位數量等)。為了實現匯入匯出的靈活性,將利用java的反射機

BufferedReaderBufferedWriter讀取寫入資料

import java.util.*; import java.io.*; public class Score{ public static void main(String args[]) throws Exception{ String path

常用的PHP函式封裝有排序資料庫操作函式

//二分查詢 function bin_sch($array, $low, $high, $k) { if ($low &lt;= $high) { $mid = intval(($low + $high) / 2); if ($array[$mid] ==

讀取檔案全部內容---C++ 寫入資料到檔案

//使用char接收---#include<fstream> #include<fstream> std::ifstream t; int length = 0; t.open("../vtkWriter.cxx"); // open

Java從CSV檔案中讀取資料寫入

.CSV檔案是以逗號分割的資料倉儲,讀取資料時從每一行中讀取一條資料元祖,也就是一條資料,再用字元分割的方式獲取表中的每一個數據項。 package com.conn.csv;

python讀取.txt檔案資料資料寫入檔案.txt

一.讀取.txt檔案中的資料到張量中 說明:將data_x.txt和data_y.txt中的資料分別讀取到x_data張量中 #!/usr/bin/python # coding=utf-8 im

SSIS遍歷xls檔案讀取寫入資料

根據業務部門提供的資料,連線業務資料庫查詢資料,並根據業務資料庫處理的資料寫入xls表。 1、開啟 Microsoft Visual Studio 2008,檔案-新建專案-新建一個“Integration Services 專案”,重新命名為:SSIS_xls。 2

C#基礎精華06(Linq To XML讀取xml檔案寫入xml

Linq To XML xml 標記語言 html  xml 側重於儲存資料   html顯示資料 註釋<!-- 註釋 --> xml沒有預定義標籤  html有 核心類XElement,一個XElement表示一個元素,new XElement(“Order”

web.xml檔案載入順序 一、 1 、啟動一個 WEB 專案的時候 WEB 容器會去讀取它的配置檔案 web.xml 讀取

web.xml 中的listener、 filter、servlet 載入順序及其詳解 一、概述 1、啟動一個WEB專案的時候,WEB容器會去讀取它的配置檔案web.xml,讀取<listener>和<context-param>兩個結點。 

用Java POI操作Excel讀取資料匯入DB2資料庫

Java操作Excel現在基本有兩種方法,就是使用JXL或POI,這兩者各有利弊,互有長短。下載地址:POI  http://jakarta.apache.org/poi/index.html                    JXL  http://www.andykh

C#讀取XML檔案資料資料儲存至xml方法

原文在百度知道中,來源於多個網友。 新浪微博:http://blog.sina.com.cn/s/blog_ad7fd0f4010180md.html (一) 儲存 var xml =XElement.Load(@"路徑");xml.Element("節點名字").AddA

tp讀取寫入

save att 讀取 name 寫入 fun ted protect 顯示 一、讀取器 控制器調用如下: //以ID的方式查詢數據$user=User::get(3);//查詢ID為3的單條數據echo $user->username;//以對象的方式顯示對應的字段

VS2013 c++連結資料庫應用儲存過程向資料庫中寫入資料

// ConsoleApplication1.cpp : 定義控制檯應用程式的入口點。 // #include "stdafx.h" #include "iomanip" using namespace std; #import "c:\Program Files\Common Files\S

java讀取檔案寫入檔案的方式(位元組流字元流)

java讀取檔案和寫入檔案的方式 以位元組為單位讀取檔案 一次讀一個位元組 一次讀多個位元組 以字元為單位讀取檔案 一次讀一個字元 一次讀多個字元 以位元組為單位讀取檔案 以位元

基於GDAL庫讀取海洋風場資料(.nc格式)c++版

        經過這一段時間的對海洋資料的處理,接觸了大量的與海洋相關的資料,例如海洋地形、海洋表面溫度、鹽度、溼度、雲場、風場等資料,除了地形資料是grd格式外,其他的都是nc格式的資料。本文將以海洋風場資料為例,進行nc格式檔案的讀取。