午夜剧场伦理_日本一道高清_国产又黄又硬_91黄色网战_女同久久另类69精品国产_妹妹的朋友在线

您的位置:首頁技術文章
文章詳情頁

使用PHP DOM-XML創建和解析XML文件

瀏覽:353日期:2022-06-03 15:38:55

使用PHP DOM-XML創建和解析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創建和解析XML文件

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

  //創建根節點,并設置一個屬性
  $root = $doc->add_root("faq" );  
  $root->setattr("page", "32" );  

  //子節點
  $one = $root->new_child("question", "");  
  //為子節點設置屬性
  $one->setattr("number", "1");  
  //question也創建子節點,并且給它賦值
  $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?" );  
  // 創建一個不直接賦值的節點
  $twoone = $two->new_child("answer", "");  
  // 然后給它單獨賦值
  $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);  

  // ------------------------------------------------------
  //現在使用xpath從XML文檔中得到內容

  $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的類型,名稱和內容
  $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上運行

PHPDOM類庫請到http://www.zend.com/download下載
標簽: XML/RSS
相關文章:
主站蜘蛛池模板: 中韩毛片 | 青草草在线视频 | 亚洲黄色一区二区三区 | 国产精品网页 | 亚洲a网站 | 免费黄色一级片 | 狠狠干在线观看 | a在线看| 日本高清一二三区 | 一级特黄色片 | 99热国内精品 | 天天天天天天天天操 | 国产天堂视频 | 亚洲在线天堂 | 日本精品视频一区二区三区 | 国产成人97精品免费看片 | 国产精品又黄又爽又色 | 久久久久一区二区三区四区 | 日日夜夜一区二区 | 欧美特级黄色大片 | 欧美三级网 | 在线观看中文字幕一区 | 黄色激情在线观看 | 日韩精品中文字幕在线播放 | 台湾久久 | 你懂的在线免费观看 | 99亚洲欲妇| 欧美黄色一区二区 | 天堂中文资源在线 | 精品一级视频 | 手机在线精品视频 | 四虎永久地址 | 中文字幕高清视频 | h视频在线看 | 日本黄色三级视频 | 成人福利视频在线观看 | 欧美一级片免费看 | 午夜影院在线 | 亚洲精品久久久久久国 | 天天看天天射 | 亚洲综合免费视频 |