2016-06-10 187 views
1

我有一个codeigniter库,用于加载一个xml文件从.xml获取信息

using simplexml_load_file();

我如何过可以得到的XML文件中的所有CDATA信息,但我不能“从XML

我怎样才能得到正确的名称=”从XML获取名称=“”

<file name="controllers/Test.php">

完整的XML

<?xml version="1.0" encoding="UTF-8" ?> 
<modification> 
    <file name="controllers/Test.php"> <!-- need to get this name=""--> 
     <operation> 
      <search><![CDATA[class Test extends CI_Controller {]]></search> 
      <add position="after"><![CDATA[public function __construct() {}]]></add> 
     </operation> 
    </file> 
</modification> 

瓦尔转储使用simplexml_load_file)的

object(SimpleXMLElement)[16] 
    public 'file' => 
    object(SimpleXMLElement)[17] 
     public '@attributes' => 
     array (size=1) 
      'name' => string 'controllers/Test.php' (length=20) 
     public 'operation' => 
     object(SimpleXMLElement)[18] 
      public 'search' => 
      object(SimpleXMLElement)[19] 
       ... 
      public 'add' => 
      object(SimpleXMLElement)[20] 
       ... 

我的图书馆

<?php 

class Mod_xml { 

    public function __construct() { 
     $this->run(); 
    } 

    public function run() { 
     $file = APPPATH . 'third_party/mods/mods_test.xml'; 

     $x = simplexml_load_file($file); 

     var_dump($x); 

     // It should some how get <file name="get this content"> 
     //$name = ; 

     $search_for_code = $x->file->operation->search; 

     $add = $x->file->operation->add; 

     $get_the_main_file_contents = file_get_contents(APPPATH . 'controllers/Test.php'); 

     $new_file_data = str_replace("$search_for_code", "$add", $get_the_main_file_contents); 

     if (!file_exists(APPPATH . 'controllers/backup/Test.php')) { 
      @copy(APPPATH . 'controllers/Test.php', APPPATH . 'controllers/backup/Test.php'); 
     } 

     if(strpos($get_the_main_file_contents, "$search_for_code") !== FALSE){ 
      file_put_contents(APPPATH . 'controllers/Test.php', $new_file_data); 
     } 
    } 
} 

回答

0

发现了如何非常简单的解决方案

$file = APPPATH . 'third_party/mods/mods_test.xml'; 

$x = simplexml_load_file($file); 

echo $x->file['name'];