2014-06-24 22 views

回答

0

從這節課:

class ExSimpleXMLElement extends SimpleXMLElement 
{ 


    public function _construct($xml){ 
     parent::_construct($xml); 
    } 

    /** 
    * Add SimpleXMLElement code into a SimpleXMLElement 
    * @param SimpleXMLElement $append 
    */ 
    public function appendXML($append) 
    { 
     if ($append) { 
      if (strlen(trim((string) $append))==0) { 
       $xml = $this->addChild($append->getName()); 
       foreach($append->children() as $child) { 
        $xml->appendXML($child); 
       } 
      } else { 
       $xml = $this->addChild($append->getName(), (string) $append); 
      }  
      foreach($append->attributes() as $n => $v) { 
       $xml->addAttribute($n, $v); 
      } 
     } 
    } 
} 

你會改變這樣的:

<?php 

class ExSimpleXMLElement 
{ 
    private $sxe; 

    public function loadXml($xml){ 
     $this->sxe = new SimpleXMLElement($xml); 
    } 

    /** 
    * Add SimpleXMLElement code into a SimpleXMLElement 
    * @param SimpleXMLElement $append 
    */ 
    public function appendXML($append) 
    { 
     if ($append) { 
      if (strlen(trim((string) $append))==0) { 
       $xml = $this->sxe->addChild($append->getName()); 
       foreach($append->children() as $child) { 
        $xml->appendXML($child); 
       } 
      } else { 
       $xml = $this->sxe->addChild($append->getName(), (string) $append); 
      }  
      foreach($append->attributes() as $n => $v) { 
       $xml->addAttribute($n, $v); 
      } 
     } 
    } 
} 

而且使用這樣的:

$this->load->library('ExSimpleXMLElement'); 

$this->exsimplexmlelement->loadXML($xml); 
+0

感謝reponder machineaddict – Corematrixx

+0

machineaddict。我嘗試了代碼,並得到錯誤圖像,這可能是?我打電話利比里亞這樣: [鏈接](https://docs.google.com/file/d/0B4eow_YS2Y1eVjFaQV9HRlNCcWM/edit) ''公共職能edit_svg(){ $這個 - >負載>庫( 'ExSimpleXMLElement'); $ file =「images/designs/svg/a.svg」; //http://www.webdeveloper.com/forum/showthread.php?165648-Editing-XML-using-PHP $ xml = simplexml_load_file($ file); //加載文件。 $ this-> exsimplexmlelement-> loadXML($ xml)'' – Corematrixx

+0

而不是'$ xml = simplexml_load_file($ file);',使用'$ xml = file_get_contents($ file);'。 'loadXML'方法需要xml作爲字符串。 – machineaddict