-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathXMLSerializer.php
51 lines (41 loc) · 1.09 KB
/
XMLSerializer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<?PHP
require_once('core/XMLBuilder.php');
/**
* XMLSerializer parsed objects to XML.
* It takes all public properties and convert it in a well done XML Structure
*
* @author Pascal Geldmacher
*/
class XMLSerializer{
private $domDocument = null;
public function __construct(){
}
/**
*
* @param mixed $object, the object to parse in a XML structure
* @return String
*
*/
public function parseObjectToXML($object=null){
if(!isset($object)){
throw new Exception('object cant be empty');
}
$this->domDocument = new DOMDocument('1.0', 'UTF-8');
$this->domDocument->formatOutput = true;
$xmlBuilder = new XMLBuilder($this->domDocument);
$root = null;
if(is_array($object)){
$rootName = array_keys($object)[0];
$root = $this->domDocument->createElement($rootName);
$arrayValues = $object[$rootName];
foreach($arrayValues as $value){
$xmlBuilder->generateXML($value, $root);
}
}else{
$root = $xmlBuilder->generateXML($object);
}
$this->domDocument->appendChild($root);
return $this->domDocument->saveXML();
}
}
?>