3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
37 * Read a well-formed XML file
41 * @return array (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.';
50 $oldLibXMLErrors = libxml_use_internal_errors();
51 libxml_use_internal_errors(TRUE);
53 $xml = simplexml_load_file($file,
54 'SimpleXMLElement', LIBXML_NOCDATA
57 $error = self
::formatErrors(libxml_get_errors());
60 libxml_use_internal_errors($oldLibXMLErrors);
63 return array($xml, $error);
67 * Read a well-formed XML file
71 * @return array (0 => SimpleXMLElement|FALSE, 1 => errorMessage|FALSE)
73 public static function parseString($string) {
74 $xml = FALSE; // SimpleXMLElement
75 $error = FALSE; // string
77 $oldLibXMLErrors = libxml_use_internal_errors();
78 libxml_use_internal_errors(TRUE);
80 $xml = simplexml_load_string($string,
81 'SimpleXMLElement', LIBXML_NOCDATA
84 $error = self
::formatErrors(libxml_get_errors());
87 libxml_use_internal_errors($oldLibXMLErrors);
89 return array($xml, $error);
97 protected static function formatErrors($errors) {
100 foreach ($errors as $error) {
101 if ($error->level
!= LIBXML_ERR_ERROR
&& $error->level
!= LIBXML_ERR_FATAL
) {
107 $parts[] = "File=$error->file";
109 $parts[] = "Line=$error->line";
110 $parts[] = "Column=$error->column";
111 $parts[] = "Code=$error->code";
113 $messages[] = implode(" ", $parts) . ": " . trim($error->message
);
116 return implode("\n", $messages);
120 * Convert an XML element to an array
122 * @pararm $obj SimpleXMLElement
127 public static function xmlObjToArray($obj) {
129 if (is_object($obj)) {
130 $obj = get_object_vars($obj);
132 if (is_array($obj)) {
133 foreach ($obj as $i => $v) {
134 if (is_object($v) ||
is_array($v)) {
135 $v = self
::xmlObjToArray($v);