3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
30 * @copyright CiviCRM LLC (c) 2004-2019
35 * Read a well-formed XML file
40 * (0 => SimpleXMLElement|FALSE, 1 => errorMessage|FALSE)
42 public static function parseFile($file) {
48 if (!file_exists($file)) {
49 $error = 'File ' . $file . ' does not exist.';
52 $oldLibXMLErrors = libxml_use_internal_errors();
53 libxml_use_internal_errors(TRUE);
55 $xml = simplexml_load_file($file,
56 'SimpleXMLElement', LIBXML_NOCDATA
59 $error = self
::formatErrors(libxml_get_errors());
62 libxml_use_internal_errors($oldLibXMLErrors);
65 return [$xml, $error];
69 * Read a well-formed XML file
74 * (0 => SimpleXMLElement|FALSE, 1 => errorMessage|FALSE)
76 public static function parseString($string) {
82 $oldLibXMLErrors = libxml_use_internal_errors();
83 libxml_use_internal_errors(TRUE);
85 $xml = simplexml_load_string($string,
86 'SimpleXMLElement', LIBXML_NOCDATA
89 $error = self
::formatErrors(libxml_get_errors());
92 libxml_use_internal_errors($oldLibXMLErrors);
94 return [$xml, $error];
102 protected static function formatErrors($errors) {
105 foreach ($errors as $error) {
106 if ($error->level
!= LIBXML_ERR_ERROR
&& $error->level
!= LIBXML_ERR_FATAL
) {
112 $parts[] = "File=$error->file";
114 $parts[] = "Line=$error->line";
115 $parts[] = "Column=$error->column";
116 $parts[] = "Code=$error->code";
118 $messages[] = implode(" ", $parts) . ": " . trim($error->message
);
121 return implode("\n", $messages);
125 * Convert an XML element to an array.
132 public static function xmlObjToArray($obj) {
134 if (is_object($obj)) {
135 $obj = get_object_vars($obj);
137 if (is_array($obj)) {
138 foreach ($obj as $i => $v) {
139 if (is_object($v) ||
is_array($v)) {
140 $v = self
::xmlObjToArray($v);