3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
36 * Read a well-formed XML file
41 * (0 => SimpleXMLElement|FALSE, 1 => errorMessage|FALSE)
43 public static function parseFile($file) {
44 $xml = FALSE; // SimpleXMLElement
45 $error = FALSE; // string
47 if (!file_exists($file)) {
48 $error = 'File ' . $file . ' does not exist.';
51 $oldLibXMLErrors = libxml_use_internal_errors();
52 libxml_use_internal_errors(TRUE);
54 $xml = simplexml_load_file($file,
55 'SimpleXMLElement', LIBXML_NOCDATA
58 $error = self
::formatErrors(libxml_get_errors());
61 libxml_use_internal_errors($oldLibXMLErrors);
64 return array($xml, $error);
68 * Read a well-formed XML file
73 * (0 => SimpleXMLElement|FALSE, 1 => errorMessage|FALSE)
75 public static function parseString($string) {
76 $xml = FALSE; // SimpleXMLElement
77 $error = FALSE; // string
79 $oldLibXMLErrors = libxml_use_internal_errors();
80 libxml_use_internal_errors(TRUE);
82 $xml = simplexml_load_string($string,
83 'SimpleXMLElement', LIBXML_NOCDATA
86 $error = self
::formatErrors(libxml_get_errors());
89 libxml_use_internal_errors($oldLibXMLErrors);
91 return array($xml, $error);
99 protected static function formatErrors($errors) {
102 foreach ($errors as $error) {
103 if ($error->level
!= LIBXML_ERR_ERROR
&& $error->level
!= LIBXML_ERR_FATAL
) {
109 $parts[] = "File=$error->file";
111 $parts[] = "Line=$error->line";
112 $parts[] = "Column=$error->column";
113 $parts[] = "Code=$error->code";
115 $messages[] = implode(" ", $parts) . ": " . trim($error->message
);
118 return implode("\n", $messages);
122 * Convert an XML element to an array.
129 public static function xmlObjToArray($obj) {
131 if (is_object($obj)) {
132 $obj = get_object_vars($obj);
134 if (is_array($obj)) {
135 foreach ($obj as $i => $v) {
136 if (is_object($v) ||
is_array($v)) {
137 $v = self
::xmlObjToArray($v);