Chinaunix首页 | 论坛 | 博客
  • 博客访问: 696092
  • 博文数量: 160
  • 博客积分: 8847
  • 博客等级: 中将
  • 技术积分: 1656
  • 用 户 组: 普通用户
  • 注册时间: 2010-11-25 16:46
个人简介

。。。。。。。。。。。。。。。。。。。。。。

文章分类

全部博文(160)

文章存档

2015年(1)

2013年(1)

2012年(4)

2011年(26)

2010年(14)

2009年(36)

2008年(38)

2007年(39)

2006年(1)

分类: 系统运维

2012-10-26 02:08:39


点击(此处)折叠或打开

  1. <?php
  2. class DomsController extends AppController{
  3.     public $xmlObject;
  4.     //使用Dom document方式操作xml文件时候,可以。。。
  5.     public function loadXml(){
  6.         $this->xmlObject = new DOMDocument();
  7.         if ($this->xmlObject->load('xml/books.xml')){
  8.             return true;
  9.         }
  10.         return false;
  11.     }
  12.     
  13.     public function readXmlByDom(){
  14.         $this->xmlObject = new DOMDocument();
  15.         if ($this->xmlObject->load('xml/books.xml')){
  16.             //read book
  17.             $response = '
      ';
    •             $books = $this->xmlObject->getElementsByTagName('book');
    •             for ($j = 0; $j < $books->length; $j++){
    •                 $response .= '

      ' . $books->item($j)->firstChild->nodeValue . '(' . $books->item($j)->firstChild->attributes->item(0)->value . ')' . '

      '
      ;
    •                 $stories = $this->xmlObject->getElementsByTagName('story');
    •                 $response .= '
        ';
      •                 for($i = 0; $i < $stories->length; $i++){
      •                     $story = $stories->item($i);
      •                     //说到底,采用dom document这种方式访问xml文件的时候,需要不断的for循环,最后取出来结果,草
      •                     //访问的时候,最好还是采用xpath或者simlexml方式比较好
      •                     $response .= '
      • ' . $story->firstChild->nodeValue . '
      • ';
      •                     $response .= '
      • ' . $story->lastChild->nodeValue . '
      • ';
      •                 }
      •                 $response .= '
      '
      ;
    •             }
    •             $response .= '
    '
    ;
  18.         }
  19.         
  20.         echo $response;
  21.         //使用simplexml遍历这颗树,比较好
  22.         $this->xmlObject = simplexml_load_file('xml/books.xml');
  23.         if (!empty($this->xmlObject)){
  24.             $response = '
      ';
    •             foreach ($this->xmlObject->book as $book){
    •                 $name = $book->name;
    •                 $year = $book->name['year'];
    •                 $response .= '

      ' . $name . '(' . $year . ')' . '

      '
      ;
    •                 foreach ($book->story as $story){
    •                     $title = $story->title;
    •                     $quote = $story->quote;
    •                     $response .= '
    • ' . $title . '
    • ';
    •                     $response .= '
    • ' . $quote . '
    • ';
    •                 }
    •             }
    •             $response .= '
    '
    ;
  25.         }
  26.         
  27.         echo $response;
  28.         
  29.         //使用xpath搜索树中的某个节点比较好
  30.         $this->xmlObject = simplexml_load_file('xml/common.xml');
  31.         //找到以后一定是一个数组
  32.         $paths = $this->xmlObject->xpath('//name');
  33.         foreach ($paths as $path){
  34.             echo $path . '->' . $path['year'] . '
    '
    ;
  35.             $stories = $this->xmlObject->xpath('//story');
  36.             foreach ($stories as $story){
  37.                 echo $story->title . '
    '
    ;
  38.                 echo $story->quote . '
    '
    ;
  39.             }
  40.         }
  41.         
  42.         
  43.         $this->autoRender = false;
  44.     }
  45.     
  46.     public function createXmlByDom(){
  47.         $this->autoRender = false;
  48.         $this->xmlObject = new DOMDocument('1.0', 'utf-8');
  49.         //root node -- books
  50.         $books = $this->xmlObject->createElement('books');
  51.         //book node
  52.         $book = $this->xmlObject->createElement('book');
  53.         //book node's attribute -- index
  54.         $index = new DOMAttr('index', '1');
  55.         $book->appendChild($index);
  56.         //name node
  57.         $name = $this->xmlObject->createElement('name', 'Maozedong');
  58.         //name node's attribute -- year
  59.         $year = new DOMAttr('year', '1920');
  60.         $name->appendChild($year);
  61.         $book->appendChild($name);
  62.         //story node
  63.         $story = $this->xmlObject->createElement('story');
  64.         $title = $this->xmlObject->createElement('title', 'Redrevolution');
  65.         $quote = $this->xmlObject->createElement('quote', 'LeaveoffHunan');
  66.         $story->appendChild($title);
  67.         $story->appendChild($quote);
  68.         $book->appendChild($story);
  69.         $books->appendChild($book);
  70.         $this->xmlObject->appendChild($books);//注意最后要把根节点append到树中,否则看到的只有一行头部信息
  71.         
  72.         if ($this->xmlObject->save('xml/books.xml') != false){
  73.             echo 'success';
  74.         }else{
  75.             echo 'error';
  76.         }
  77.     }
  78.     
  79.     private function getIndex($index){
  80.         if (empty($index)){
  81.             if ($this->request->is('get')){
  82.                 $index = $this->request->query['index'];
  83.             }
  84.                 
  85.             if($this->request->is('post')){
  86.                 $index = $this->request->data('index');
  87.             }
  88.         }
  89.         return $index;
  90.     }
  91.     
  92.     public function updateXmlByDom($index = NULL){
  93.         $index = $this->getIndex($index);
  94.         //使用Dom document更新
  95.         $this->xmlObject = new DOMDocument();
  96.         $filename = 'xml/books.xml';
  97.         if ($this->xmlObject->load($filename)){
  98.             $books = $this->xmlObject->getElementsByTagName('book');
  99.             for($i = 0; $i < $books->length; $i++){
  100.                 //book 只有一个attribute是index
  101.                 $book = $books->item($i);
  102.                 $myIndex = $book->attributes->item(0)->value;
  103.                 if ($myIndex == $index){
  104.                     $name = $this->xmlObject->createElement('name', 'new book name');
  105.                     $nameAttr = new DOMAttr('year', '1988');
  106.                     $story = $this->xmlObject->createElement('story');
  107.                     $title = $this->xmlObject->createElement('title', 'new book title');
  108.                     $quote = $this->xmlObject->createElement('quote', 'new book quote');
  109.                     $book->appendChild($name);
  110.                     $story->appendChild($title);
  111.                     $story->appendChild($quote);
  112.                     
  113.                     if ($this->xmlObject->save($filename)){
  114.                         echo "update success.";
  115.                     }else{
  116.                         echo "update error.";
  117.                     }
  118.                 }
  119.             }
  120.         }
  121.     }
  122.     
  123.     public function deleteXmlNodeByDom($index = NULL){
  124.         $index = $this->getIndex($index);
  125.         //使用Dom document删除
  126.         $this->xmlObject = new DOMDocument();
  127.         $filename = 'xml/books.xml';
  128.         if ($this->xmlObject->load($filename)){
  129.             $books = $this->xmlObject->getElementsByTagName('book');
  130.             for($i = 0; $i < $books->length; $i++){
  131.                 //book 只有一个attribute是index
  132.                 $book = $books->item($i);
  133.                 $myIndex = $book->attributes->item(0)->value;
  134.                 if ($myIndex == $index){
  135.                     //这句很诡异啊
  136.                     $book->parentNode->removeChild($book);
  137.                     if ($this->xmlObject->save($filename)){
  138.                         echo "update success.";
  139.                     }else{
  140.                         echo "update error.";
  141.                     }    
  142.                 }
  143.             }
  144.         }
  145.     }
  146. }

阅读(1753) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~