主頁 > 知識庫 > 使用PHP DOM-XML創(chuàng)建和解析XML文件

使用PHP DOM-XML創(chuàng)建和解析XML文件

熱門標(biāo)簽:銀行業(yè)務(wù) 呼叫中心市場需求 服務(wù)器配置 網(wǎng)站文章發(fā)布 智能手機(jī) 檢查注冊表項(xiàng) 鐵路電話系統(tǒng) 美圖手機(jī)

使用PHP DOM-XML創(chuàng)建和解析XML文件 ?php  
  /**
  * Topic:       Create and parse XML files using PHP DOM-XML
  * Source:      http://www.php.net/domxml
  * Reference:   http://www.zugeschaut-und-mitgebaut.de/php/extension.domxml.html
  * Author:      urs@circle.ch, 16-1-2001
  *
  */  
// 使用PHP DOM-XML創(chuàng)建和解析XML文件

  //創(chuàng)建XML文檔對象;以后的處理過程將在此基礎(chǔ)上進(jìn)行
  $doc = new_xmldoc("1.0" );  

  //創(chuàng)建根節(jié)點(diǎn),并設(shè)置一個屬性
  $root = $doc->add_root("faq" );  
  $root->setattr("page", "32" );  

  //子節(jié)點(diǎn)
  $one = $root->new_child("question", "");  
  //為子節(jié)點(diǎn)設(shè)置屬性
  $one->setattr("number", "1");  
  //question也創(chuàng)建子節(jié)點(diǎn),并且給它賦值
  $one->new_child("text", "1. Where to get libxml-2.0.0?");  
  $one->new_child("answer", "You can download the latest
  release of libxml   either as a source archive or
  RPM package from http://www.xmlsoft.org.
  The current version is libxml2-2.2.1." );  

  $two = $root->new_child("question", "" );  
  $two->setattr("number", "2");  
  $two->new_child("text", "2. How to configure PHP4?" );  
  // 創(chuàng)建一個不直接賦值的節(jié)點(diǎn)
  $twoone = $two->new_child("answer", "");  
  // 然后給它單獨(dú)賦值
  $twoone->set_content("DIR is the libxml install directory
  (if you just use --with-dom it defaults
  to /usr), I needed to use --with-dom=/usr/local" );  

  $three = $root->new_child("question", "" );  
  $three->setattr("number", "7" );  
  $three->new_child("text", "7. How to use DOM XML function ?" );  
  $three->new_child("answer", "Read this document source for
  a simple example." );  

  //輸出到Browser
  print("pre>".htmlspecialchars($doc->dumpmem() )."/pre>" );  

  // write to file
  //寫回到文件
  $fp = fopen("test_dom.xml", "w+" );  
  fwrite($fp, $doc->dumpmem(), strlen($doc->dumpmem() ));  
  fclose($fp);  

  // ------------------------------------------------------
  //現(xiàn)在使用xpath從XML文檔中得到內(nèi)容

  $doc = xmldoc(join("", file("test_dom.xml")) );  
  $ctx = xpath_new_context($doc );  

  //所有對象
  $foo = xpath_eval($ctx, "http://child::*");  
  print_r($foo);  
  print("br/>br/>");  
  //text node 對象
  $foo = xpath_eval($ctx, "http://text");  
  print_r($foo);  
  print("br/>br/>");  
  // 第一個text node對象
  $foo = xpath_eval($ctx, "http://text[1]");  
  print_r($foo);  
  print("br/>br/>");  
  // 第二個text node對象
  $foo = xpath_eval($ctx, "http://text[2]");  
  print_r($foo);  
  print("br/>br/>");  
  // 第三個answer對象
  $foo = xpath_eval($ctx, "http://answer[3]");  
  print_r($foo);  
  print("br/>br/>");  

  //第三個text node的類型,名稱和內(nèi)容
  $foo = xpath_eval($ctx, "http://text[3]");  
  $tmp = $foo->nodeset;  
  print_r($tmp);  
  print("br/>");  
  print($tmp[0]->type) . "; ";  
  print($tmp[0]->name) . "; ";  
  print($tmp[0]->content);  

?>  

需要說明,PHP DOM 只能在PHP  PHP4.0.x + linux上運(yùn)行

PHPDOM類庫請到http://www.zend.com/download下載
您可能感興趣的文章:
  • 解析php DOMElement 操作xml 文檔的實(shí)現(xiàn)代碼
  • PHP基于DOMDocument解析和生成xml的方法分析
  • PHP XML操作類DOMDocument
  • php中DOMDocument簡單用法示例代碼(XML創(chuàng)建、添加、刪除、修改)
  • DOM基礎(chǔ)及php讀取xml內(nèi)容操作的方法
  • php中使用DOM類讀取XML文件的實(shí)現(xiàn)代碼
  • PHP5中使用DOM控制XML實(shí)現(xiàn)代碼
  • 用PHP讀取和編寫XML DOM的實(shí)現(xiàn)代碼
  • 詳解PHP原生DOM對象操作XML的方法
  • 用PHP實(shí)現(xiàn)讀取和編寫XML DOM代碼
  • PHP使用DOM對XML解析處理操作示例

標(biāo)簽:長治 河南 紅河 滄州 樂山 沈陽 新疆 上海

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《使用PHP DOM-XML創(chuàng)建和解析XML文件》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266