OXID eShop CE  4.10.2
 All Classes Namespaces Files Functions Variables Pages
oxsimplexml.php
Go to the documentation of this file.
1 <?php
2 
25 {
26 
35  public function objectToXml($oInput, $sDocument)
36  {
37  $oXml = new SimpleXMLElement("<?xml version=\"1.0\" encoding=\"utf-8\"?><$sDocument/>");
38  $this->_addSimpleXmlElement($oXml, $oInput);
39 
40  return $oXml->asXml();
41  }
42 
50  public function xmlToObject($sXml)
51  {
52  return simplexml_load_string($sXml);
53  }
54 
64  protected function _addSimpleXmlElement($oXml, $oInput, $sPreferredKey = null)
65  {
66  $aElements = is_object($oInput) ? get_object_vars($oInput) : (array) $oInput;
67 
68  foreach ($aElements as $sKey => $mElement) {
69  $oXml = $this->_addChildNode($oXml, $sKey, $mElement, $sPreferredKey);
70  }
71 
72  return $oXml;
73  }
74 
85  protected function _addChildNode($oXml, $sKey, $mElement, $sPreferredKey = null)
86  {
87  $aAttributes = array();
88  if (is_array($mElement) && array_key_exists('attributes', $mElement) && is_array($mElement['attributes'])) {
89  $aAttributes = $mElement['attributes'];
90  $mElement = $mElement['value'];
91  }
92 
93  if (is_object($mElement) || is_array($mElement)) {
94  if (is_int(key($mElement))) {
95  $this->_addSimpleXmlElement($oXml, $mElement, $sKey);
96  } else {
97  $oChildNode = $oXml->addChild($sPreferredKey? $sPreferredKey : $sKey);
98  $this->_addNodeAttributes($oChildNode, $aAttributes);
99  $this->_addSimpleXmlElement($oChildNode, $mElement);
100  }
101  } else {
102  $oChildNode = $oXml->addChild($sPreferredKey? $sPreferredKey : $sKey, $mElement);
103  $this->_addNodeAttributes($oChildNode, $aAttributes);
104  }
105 
106  return $oXml;
107  }
108 
117  protected function _addNodeAttributes($oNode, $aAttributes)
118  {
119  $aAttributes = (array) $aAttributes;
120  foreach ($aAttributes as $sKey => $sValue) {
121  $oNode->addAttribute($sKey, $sValue);
122  }
123 
124  return $oNode;
125  }
126 }