3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
14 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 * Read a well-formed XML file
24 * (0 => SimpleXMLElement|FALSE, 1 => errorMessage|FALSE)
26 public static function parseFile($file) {
32 if (!file_exists($file)) {
33 $error = 'File ' . $file . ' does not exist.';
36 $oldLibXMLErrors = libxml_use_internal_errors();
37 libxml_use_internal_errors(TRUE);
39 $xml = simplexml_load_file($file,
40 'SimpleXMLElement', LIBXML_NOCDATA
43 $error = self
::formatErrors(libxml_get_errors());
46 libxml_use_internal_errors($oldLibXMLErrors);
49 return [$xml, $error];
53 * Read a well-formed XML file
58 * (0 => SimpleXMLElement|FALSE, 1 => errorMessage|FALSE)
60 public static function parseString($string) {
66 $oldLibXMLErrors = libxml_use_internal_errors();
67 libxml_use_internal_errors(TRUE);
69 $xml = simplexml_load_string($string,
70 'SimpleXMLElement', LIBXML_NOCDATA
73 $error = self
::formatErrors(libxml_get_errors());
76 libxml_use_internal_errors($oldLibXMLErrors);
78 return [$xml, $error];
86 protected static function formatErrors($errors) {
89 foreach ($errors as $error) {
90 if ($error->level
!= LIBXML_ERR_ERROR
&& $error->level
!= LIBXML_ERR_FATAL
) {
96 $parts[] = "File=$error->file";
98 $parts[] = "Line=$error->line";
99 $parts[] = "Column=$error->column";
100 $parts[] = "Code=$error->code";
102 $messages[] = implode(" ", $parts) . ": " . trim($error->message
);
105 return implode("\n", $messages);
109 * Convert an XML element to an array.
116 public static function xmlObjToArray($obj) {
118 if (is_object($obj)) {
119 $obj = get_object_vars($obj);
121 if (is_array($obj)) {
122 foreach ($obj as $i => $v) {
123 if (is_object($v) ||
is_array($v)) {
124 $v = self
::xmlObjToArray($v);