搜尋
首頁後端開發php教程php的XML文件解释类应用实例,phpxml类应用实例_PHP教程

php的XML文件解释类应用实例,phpxml类应用实例

本文实例讲述了php的XML文件解释类及其用法,是非常实用的技巧。分享给大家供大家参考。具体如下:

XMLParser.class.php类文件如下:

<&#63;php 
/** XML 文件分析类 
*  Date:  2013-02-01 
*  Author: fdipzone 
*  Ver:  1.0 
* 
*  func: 
*  loadXmlFile($xmlfile)   读入xml文件输出Array 
*  loadXmlString($xmlstring) 读入xmlstring 输出Array 
*/ 
 
class XMLParser{ 
 
  /** 读取xml文件 
  * @param String $xmlfile 
  * @return Array 
  */ 
  public function loadXmlFile($xmlfile){ 
    // get xmlfile content 
    $xmlstring = file_exists($xmlfile)&#63; file_get_contents($xmlfile) : ''; 
    // parser xml 
    list($flag, $data) = $this->parser($xmlstring); 
    return $this->response($flag, $data); 
  } 
 
  /** 读取xmlstring 
  * @param String $xmlstring 
  * @return Array 
  */ 
  public function loadXmlString($xmlstring){ 
    // parser xml 
    list($flag, $data) = $this->parser($xmlstring); 
    return $this->response($flag, $data); 
  } 
 
  /** 解释xml内容 
  * @param  String $xmlstring 
  * @return Array 
  */ 
  private function parser($xmlstring){ 
    $flag = false; 
    $data = array(); 
    // check xml format 
    if($this->checkXmlFormat($xmlstring)){ 
      $flag = true; 
      // xml to object 
      $data = simpleXML_load_string($xmlstring, 'SimpleXMLElement', LIBXML_NOCDATA); 
      // object to array 
      $this->objectToArray($data); 
    } 
    return array($flag, $data); 
  } 
 
  /** 检查xml格式是否正确 
  * @param String $xmlstring 
  * @return boolean 
  */ 
  private function checkXmlFormat($xmlstring){ 
    if($xmlstring==''){ 
      return false; 
    } 
    $xml_parser_obj = xml_parser_create(); 
 
    if(xml_parse_into_struct($xml_parser_obj, $xmlstring, $vals, $indexs)===1){ // 1:success 0:fail 
      return true; 
    }else{ 
      return false; 
    } 
  } 
 
  /** object 转 Array 
  * @param object $object 
  * @return Array 
  */ 
  private function objectToArray(&$object){ 
     
    $object = (array)$object; 
     
    foreach($object as $key => $value){ 
      if($value==''){ 
        $object[$key] = ""; 
      }else{ 
        if(is_object($value) || is_array($value)){ 
          $this->objectToArray($value); 
          $object[$key] = $value; 
        } 
      } 
    } 
  } 
 
  /** 输出返回 
  * @param boolean $flag true:false 
  * @param Array  $data 转换后的数据 
  * @return Array 
  */ 
  private function response($flag=false, $data=array()){ 
    return array($flag, $data); 
  } 
} 
&#63;>

Demo示例程序如下:

<&#63;php 
require "XMLParser.class.php"; 
 
$xmlfile = 'file.xml'; 
$xmlstring = '<&#63;xml version="1.0" encoding="utf-8"&#63;> 
<xmlroot> 
<status>1000</status> 
<info></info> 
<result><id>100</id> 
<name>fdipzone</name> 
<gender>1</gender> 
<age>28</age> 
</result> 
</xmlroot>'; 
echo '<pre class="brush:php;toolbar:false">'; 
$xml_parser = new XMLParser(); 
echo "response xmlfile\r\n"; 
list($flag, $xmldata) = $xml_parser->loadXmlFile($xmlfile); 
if($flag){ 
  print_r($xmldata); 
} 
echo "response xmlstring\r\n"; 
list($flag, $xmldata) = $xml_parser->loadXmlString($xmlstring); 
if($flag){ 
  print_r($xmldata); 
} 
echo '
'; ?>

关于PHP的XML预定义常量可参考官方文档:
http://www.php.net/manual/en/libxml.constants.php

希望本文所述对大家PHP程序设计的学习有所帮助。

php怎生成xml文件

#使用dom生成xml,注意生成的xml中会没有空格。
$dom=new DOMDocument('1.0','utf-8');
$path="test.xml"; // $path 为xml文件的存储路径。

$module=$dom->createElement('newmodule');// root node
$dom->appendChild($module);

$year=$dom->createElement('year'); // add attribute node
$name=$dom->createAttribute('name');
$name->nodeValue="最新动态";
$year->setAttributeNode($name);
$module->appendChild($year);

$news=$dom->createElement('news');
$year->appendChild($news);

$date=$dom->createElement('date');
$date_value=$dom->createTextNode('01-24');
$date->appendChild($date_value);
$news->appendChild($date);

$title=$dom->createElement('title');
$title_value=$dom->createTextNode('');
$title->appendChild($title_value);
$news->appendChild($title);

$info=$dom->createElement('info');
$info_value=$dom->createTextNode(' ]]>');
$info->appendChild($info_value);
$news->appendChild($info);

echo $dom->saveXML();
$dom->save($path);
?>
 

读取php文件中的xml内容

dim xml,objNode,objAtr,nCntChd,nCntAtr
Set xml=Server.CreateObject("Microsoft.XMLDOM")
xml.Async=False
xml.Load(Server.MapPath("test.xml"))

Set objNode=xml.documentElement
nCntChd=objNode.ChildNodes.length-1

'这个可以定义asp读取xml文件的那一个值,通过传递这个值来确定读取的数据

for i=0 to nCntChd
set objAtr=objNode.ChildNodes.item(i)
nCntAtr=objAtr.Attributes.length-1

'历遍一条记录里面的所有的记录项,记录是从0开始的

for j=0 to nCntAtr
response.write objAtr.Attributes.item(j).Text&"
"
next
response.write "
"
next

Set objAtr=Nothing
Set objNode=Nothing
Set xml=Nothing
%>
参考资料:homepage.yesky.com/369/3074869.shtml
 

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/882903.htmlTechArticlephp的XML文件解释类应用实例,phpxml类应用实例 本文实例讲述了php的XML文件解释类及其用法,是非常实用的技巧。分享给大家供大家参考。具...
陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
簡單地說明PHP會話的概念。簡單地說明PHP會話的概念。Apr 26, 2025 am 12:09 AM

phpsessionstrackuserdataacrossmultiplepagerequestsusingauniqueIdStoredInAcookie.here'showtomanageThemeffectionaly:1)startAsessionWithSessionWwithSession_start()和stordoredAtain $ _session.2)

您如何循環中存儲在PHP會話中的所有值?您如何循環中存儲在PHP會話中的所有值?Apr 26, 2025 am 12:06 AM

在PHP中,遍歷會話數據可以通過以下步驟實現:1.使用session_start()啟動會話。 2.通過foreach循環遍歷$_SESSION數組中的所有鍵值對。 3.處理複雜數據結構時,使用is_array()或is_object()函數,並用print_r()輸出詳細信息。 4.優化遍歷時,可採用分頁處理,避免一次性處理大量數據。這將幫助你在實際項目中更有效地管理和使用PHP會話數據。

說明如何使用會話進行用戶身份驗證。說明如何使用會話進行用戶身份驗證。Apr 26, 2025 am 12:04 AM

會話通過服務器端的狀態管理機制實現用戶認證。 1)會話創建並生成唯一ID,2)ID通過cookies傳遞,3)服務器存儲並通過ID訪問會話數據,4)實現用戶認證和狀態管理,提升應用安全性和用戶體驗。

舉一個如何在PHP會話中存儲用戶名的示例。舉一個如何在PHP會話中存儲用戶名的示例。Apr 26, 2025 am 12:03 AM

Tostoreauser'snameinaPHPsession,startthesessionwithsession_start(),thenassignthenameto$_SESSION['username'].1)Usesession_start()toinitializethesession.2)Assigntheuser'snameto$_SESSION['username'].Thisallowsyoutoaccessthenameacrossmultiplepages,enhanc

哪些常見問題會導致PHP會話失敗?哪些常見問題會導致PHP會話失敗?Apr 25, 2025 am 12:16 AM

PHPSession失效的原因包括配置錯誤、Cookie問題和Session過期。 1.配置錯誤:檢查並設置正確的session.save_path。 2.Cookie問題:確保Cookie設置正確。 3.Session過期:調整session.gc_maxlifetime值以延長會話時間。

您如何在PHP中調試與會話相關的問題?您如何在PHP中調試與會話相關的問題?Apr 25, 2025 am 12:12 AM

在PHP中調試會話問題的方法包括:1.檢查會話是否正確啟動;2.驗證會話ID的傳遞;3.檢查會話數據的存儲和讀取;4.查看服務器配置。通過輸出會話ID和數據、查看會話文件內容等方法,可以有效診斷和解決會話相關的問題。

如果session_start()被多次調用會發生什麼?如果session_start()被多次調用會發生什麼?Apr 25, 2025 am 12:06 AM

多次調用session_start()會導致警告信息和可能的數據覆蓋。 1)PHP會發出警告,提示session已啟動。 2)可能導致session數據意外覆蓋。 3)使用session_status()檢查session狀態,避免重複調用。

您如何在PHP中配置會話壽命?您如何在PHP中配置會話壽命?Apr 25, 2025 am 12:05 AM

在PHP中配置會話生命週期可以通過設置session.gc_maxlifetime和session.cookie_lifetime來實現。 1)session.gc_maxlifetime控制服務器端會話數據的存活時間,2)session.cookie_lifetime控制客戶端cookie的生命週期,設置為0時cookie在瀏覽器關閉時過期。

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

DVWA

DVWA

Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

WebStorm Mac版

WebStorm Mac版

好用的JavaScript開發工具

Atom編輯器mac版下載

Atom編輯器mac版下載

最受歡迎的的開源編輯器

EditPlus 中文破解版

EditPlus 中文破解版

體積小,語法高亮,不支援程式碼提示功能

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。