Merge pull request #5292 from jitendrapurohit/CRM-15933
[civicrm-core.git] / CRM / Utils / XML.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
6a488035 5 +--------------------------------------------------------------------+
06b69b18 6 | Copyright CiviCRM LLC (c) 2004-2014 |
6a488035
TO
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
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. |
13 | |
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. |
18 | |
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 * @package CRM
06b69b18 30 * @copyright CiviCRM LLC (c) 2004-2014
6a488035
TO
31 * $Id$
32 */
6a488035
TO
33class CRM_Utils_XML {
34
35 /**
36 * Read a well-formed XML file
37 *
f4aaa82a
EM
38 * @param $file
39 *
a6c01b45
CW
40 * @return array
41 * (0 => SimpleXMLElement|FALSE, 1 => errorMessage|FALSE)
6a488035
TO
42 */
43 public static function parseFile($file) {
44 $xml = FALSE; // SimpleXMLElement
45 $error = FALSE; // string
46
353ffa53 47 if (!file_exists($file)) {
6a488035 48 $error = 'File ' . $file . ' does not exist.';
0db6c3e1
TO
49 }
50 else {
6a488035
TO
51 $oldLibXMLErrors = libxml_use_internal_errors();
52 libxml_use_internal_errors(TRUE);
53
54 $xml = simplexml_load_file($file,
55 'SimpleXMLElement', LIBXML_NOCDATA
56 );
57 if ($xml === FALSE) {
58 $error = self::formatErrors(libxml_get_errors());
59 }
60
61 libxml_use_internal_errors($oldLibXMLErrors);
62 }
63
64 return array($xml, $error);
65 }
66
67 /**
68 * Read a well-formed XML file
69 *
f4aaa82a
EM
70 * @param $string
71 *
a6c01b45
CW
72 * @return array
73 * (0 => SimpleXMLElement|FALSE, 1 => errorMessage|FALSE)
6a488035
TO
74 */
75 public static function parseString($string) {
76 $xml = FALSE; // SimpleXMLElement
77 $error = FALSE; // string
78
79 $oldLibXMLErrors = libxml_use_internal_errors();
80 libxml_use_internal_errors(TRUE);
81
82 $xml = simplexml_load_string($string,
83 'SimpleXMLElement', LIBXML_NOCDATA
84 );
85 if ($xml === FALSE) {
86 $error = self::formatErrors(libxml_get_errors());
87 }
88
89 libxml_use_internal_errors($oldLibXMLErrors);
90
91 return array($xml, $error);
92 }
93
5bc392e6
EM
94 /**
95 * @param $errors
96 *
97 * @return string
98 */
6a488035
TO
99 protected static function formatErrors($errors) {
100 $messages = array();
101
102 foreach ($errors as $error) {
103 if ($error->level != LIBXML_ERR_ERROR && $error->level != LIBXML_ERR_FATAL) {
104 continue;
105 }
106
107 $parts = array();
108 if ($error->file) {
e7292422 109 $parts[] = "File=$error->file";
6a488035
TO
110 }
111 $parts[] = "Line=$error->line";
112 $parts[] = "Column=$error->column";
113 $parts[] = "Code=$error->code";
114
115 $messages[] = implode(" ", $parts) . ": " . trim($error->message);
116 }
117
118 return implode("\n", $messages);
119 }
120
121 /**
fe482240 122 * Convert an XML element to an array.
6a488035 123 *
77855840
TO
124 * @param $obj
125 * SimpleXMLElement.
f4aaa82a 126 *
6a488035
TO
127 * @return array
128 */
129 public static function xmlObjToArray($obj) {
130 $arr = array();
131 if (is_object($obj)) {
132 $obj = get_object_vars($obj);
133 }
134 if (is_array($obj)) {
135 foreach ($obj as $i => $v) {
136 if (is_object($v) || is_array($v)) {
137 $v = self::xmlObjToArray($v);
138 }
139 if (empty($v)) {
140 $arr[$i] = NULL;
141 }
142 else {
143 $arr[$i] = $v;
144 }
145 }
146 }
147 return $arr;
148 }
96025800 149
6a488035 150}