我有一个codeigniter库,用于加载xml文件
使用simplexml_load_file();
我怎样才能获取xml文件中的所有cdata信息,但我无法从xml获取name =“”
如何从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>
Simple Dump of 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 :(得分:0)
找出非常简单的解决方案
ListView