2014-06-24 75 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