File: xml.class.php

package info (click to toggle)
glpi 0.68.2-1etch0.2
  • links: PTS
  • area: main
  • in suites: etch
  • size: 7,476 kB
  • ctags: 9,739
  • sloc: php: 69,695; sql: 3,514; sh: 175; makefile: 61
file content (192 lines) | stat: -rw-r--r-- 5,219 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
<?php

/*
 * @version $Id: xml.class.php 3798 2006-08-22 15:12:55Z moyo $
 -------------------------------------------------------------------------
 GLPI - Gestionnaire Libre de Parc Informatique
 Copyright (C) 2003-2006 by the INDEPNET Development Team.

 http://indepnet.net/   http://glpi-project.org
 -------------------------------------------------------------------------

 LICENSE

 This file is part of GLPI.

 GLPI is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or
 (at your option) any later version.

 GLPI is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with GLPI; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 --------------------------------------------------------------------------
 */

// ----------------------------------------------------------------------
// Based on the original file:
//* Author	: Roberto B.               *
//* E-Mail	: roberto.butti@libero.it  *
//* Version	: 1.0.3                    *
// Purpose of file:
// ----------------------------------------------------------------------




class XML 
{
	var $SqlString;
	var $IsError; 		// 1 there is a problem !!!
	var $ErrorString; 	// If there is an error, this string explains it
	var $Type; 		// Which format do you want your XML ?
	var $FilePath;  //path where the file will be saved.

	// HERE I explain $Type

	/* For Example :
	   1 (default) each value are in a tag called data
	   <dataxml>
	   <row>
	   <data>value field1 row1</data>
	   <data>value field2 row1</data>
	   <data>value field3 row1</data>
	   </row>
	   <row>
	   <data>value field1 row2</data>
	   <data>value field2 row2</data>
	   <data>value field3 row2</data>
	   </row>
	   </dataxml>

	   2 each value is in a tag called dataN , where N is a position of field
	   <dataxml>
	   <row>
	   <data1>value field1 row1</data1>
	   <data2>value field2 row1</data2>
	   <data3>value field3 row1</data3>
	   </row>
	   <row>
	   <data1>value field1 row2</data1>
	   <data2>value field2 row2</data2>
	   <data3>value field3 row2</data3>
	   </row>
	   </dataxml>

	   3 each value is in a tag called with the name of field
	   <dataxml>
	   <row>
	   <fieldname1>value field1 row1</fieldname1>
	   <fieldname2>value field2 row1</fieldname2>
	   <fieldname3>value field3 row1</fieldname3>
	   </row>
	   <row>
	   <fieldname1>value field1 row2</fieldname1>
	   <fieldname2>value field2 row2</fieldname2>
	   <fieldname3>value field3 row2</fieldname3>
	   </row>
	   </dataxml>

	   4 each value is in a tag with an attributes called fieldname with the name of field
	   <dataxml>
	   <row>
	   <data fieldname="fieldname1">value field1 row1</data>
	   <data fieldname="fieldname2">value field1 row1</data>
	   <data fieldname="fieldname3">value field1 row1</data>
	   </row>
	   <row>
	   <data fieldname="fieldname1">value field1 row2</data>
	   <data fieldname="fieldname2">value field1 row2</data>
	   <data fieldname="fieldname3">value field1 row2</data>
	   </row>
	   </dataxml>


	 */

	function XML()
	{
		// Initialize the values with DEFAULT value
		$this->IsError=0;
		$this->Type=1;
		$this->ErrorString="NO errors ;)";
		$this->SqlString="";
	}


	function DoXML()
	{
		global $db;
		$fp = fopen($this->FilePath,'wb');
		fputs($fp, "<?xml version=\"1.0\"?>\n");
		fputs($fp, "<dataxml>\n");

		foreach($this->SqlString as $strqry){
			if ($strqry==""){
				$this->IsError=1;
				$this->ErrorString="Error the query can't be a null string";
				return -1;
			}
			$result = $db->query($strqry);

			if ($result==FALSE){
				$this->IsError=1;
				$this->ErrorString="Error in SQL Query : ".$strqry;
				return -1;
			}
			// OK... let's create XML ;)
			fputs($fp, "	<fields>\n");
			$i = 0;
			$FieldsVector=array();
			while ($i < $db->num_fields ($result)){
				$name = $db->field_name($result,$i);
				fputs($fp, "		<field>".$name."</field>\n");
				$FieldsVector[]=$name;
				$i++;
			}

			fputs($fp, "	</fields>\n");
			// And NOW the Data ...
			fputs($fp, "	<rows>\n");
			while ($row = $db->fetch_array ($result)){
				fputs($fp, "		<row>\n");
				for ($j=0; $j<$i; $j++){
					$FieldName="";			// Name of TAG
					$Attributes="";
					switch ($this->Type){
						case 1:
							$FieldName="data";
							break;
						case 2:
							$FieldName="data".$j;
							break;
						case 3:
							$FieldName=$FieldsVector[$j];
							break;
						case 4:
							$FieldName="data";
							$Attributes=" fieldname=\"".$FieldsVector[$j]."\"";
					}
					fputs($fp, "			<".$FieldName.$Attributes.">".utf8_encode(htmlspecialchars($row[$j]))."</".$FieldName.">\n");
				}
				fputs($fp, "		</row>\n");
			}
			fputs($fp, "	</rows>\n");

			$db->free_result($result);
		}
		fputs($fp, "</dataxml>");
		//OK free ...  ;)
		fclose($fp);

	} // End  Function : DoXML
} // Fine Class XML


?>