[ Index ]

MailPress 544

[ Index ]     [ Classes ]     [ Functions ]     [ Variables ]     [ Constants ]     [ Statistics ]    

title

Body

[close]

/mp-includes/class/ -> MP_Xml.class.php (source)

   1  <?php
   2  class MP_Xml
   3  {
   4  	function __construct($xml)
   5      {
   6          $this->dom = New DOMDocument();
   7          $this->dom->loadXML($xml);
   8          $this->root = $this->dom->documentElement;
   9  
  10          $this->object = new stdClass();
  11          $this->getElement($this->root, $this->object);
  12      }
  13  
  14  	function get_xml($xmlObject) 
  15      {
  16          unset($xmlObject->textValue);
  17          $dom = new DomDocument();
  18          $root = $dom->createElement($xmlObject->name);
  19          $dom->appendChild($root);
  20          $this->setElement($dom, $xmlObject, $root);
  21          return $dom->saveXML();
  22      }
  23  
  24  	public static function sanitize($string, $parent = false)
  25      {
  26          $xml_header = '<?xml version="1.0"?>';
  27  
  28          if ($parent) $parent = trim($parent);
  29  
  30          $xml = '';
  31          if (strpos($string, '<?xml') === false)     $xml .= $xml_header;
  32          if ($parent)                    $xml .= "<$parent>";
  33          $xml .= trim($string);
  34          if ($parent)                    $xml .= "</$parent>";
  35  
  36          if (simplexml_load_string($xml)) return $xml;
  37  
  38          $xml = str_replace($string, utf8_encode($string), $xml);
  39          if (simplexml_load_string($xml)) return $xml;
  40  
  41          return false;
  42      }
  43  
  44  /* from http://eusebius.developpez.com/php5dom/ */
  45  
  46  	function getElement($dom_element, $object_element)
  47      {
  48          $object_element->name = $dom_element->nodeName;
  49          $object_element->textValue = (isset($dom_element->firstChild->nodeValue)) ? trim($dom_element->firstChild->nodeValue) : '';
  50          if ($dom_element->hasAttributes()) 
  51          {
  52              $object_element->attributes = array();
  53              foreach($dom_element->attributes as $attName=>$dom_attribute) $object_element->attributes[$attName] = $dom_attribute->value;
  54          }
  55          if ($dom_element->childNodes->length > 1) 
  56          {
  57              $object_element->children = array();
  58              foreach($dom_element->childNodes as $dom_child) 
  59              {
  60                  if ($dom_child->nodeType == XML_ELEMENT_NODE) 
  61                  {
  62                      $child_object = new stdClass();
  63                      $this->getElement($dom_child, $child_object);
  64                      array_push($object_element->children, $child_object);
  65                  }
  66              }
  67          }
  68      }
  69  
  70  	function setElement($dom_document, $object_element, $dom_element)
  71      {
  72          if (isset($object_element->textValue)) 
  73          {
  74              $cdata = $dom_document->createTextNode($object_element->textValue);
  75              $dom_element->appendChild($cdata);
  76          }
  77          if (isset($object_element->attributes)) foreach($object_element->attributes as $attName=>$attValue) $dom_element->setAttribute($attName, $attValue);
  78          if (isset($object_element->children)) 
  79          {
  80              foreach($object_element->children as $childObject) 
  81              {
  82                  $child = $dom_document->createElement($childObject->name);
  83                  $this->setElement($dom_document, $childObject, $child);
  84                  $dom_element->appendChild($child);            
  85              }
  86          }    
  87      }
  88  }


Generated: Thu Apr 28 18:38:52 2016 Cross-referenced by PHPXref 0.7.1