Platform  3.1
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
fdl_xml.php
Go to the documentation of this file.
1 <?php
2 /*
3  * @author Anakeen
4  * @license http://creativecommons.org/licenses/by-nc-sa/2.0/fr/ Anakeen - licence CC
5  * @package FDL
6 */
7 /**
8  * XML Utilities for freedom
9  *
10  * @author Anakeen 2000
11  * @version $Id: fdl_xml.php,v 1.3 2005/09/22 16:34:44 eric Exp $
12  * @license http://creativecommons.org/licenses/by-nc-sa/2.0/fr/ Anakeen - licence CC
13  * @package FDL
14  * @subpackage
15  */
16 /**
17  */
18 //============================== XML =======================
19 
20 /**
21  * construct document from xml
22  * @param string $dbaccess database specification
23  * @param string $xml the xml structure
24  * @param string $famid identificator of family (need correspond to xml)
25  * @param bool $encoded true is xml is encoded
26  * @return Doc the document (view like a temporary document);
27  */
28 function fromxml($dbaccess, $xml, $famid, $encoded = false)
29 {
30  if ($encoded) $xml = base64_decode(($xml));
31  $xml2 = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\" standalone=\"yes\" ?>";
32  $xml2.= str_replace("<BR>", "<BR/>", $xml);
33  $idoc = createDoc($dbaccess, $famid, false); ///new doc
34  $idoc->doctype = 'T';
35 
36  _fromxml($xml2, $idoc);
37  $idoc->Add();
38  return $idoc;
39 }
40 
41 function _fromxml($xml, &$idoc)
42 {
43  global $action;
44  $fp = $xml;
45  global $value; //used to stock value of one attribut (is string type)
46  global $tabvalues; //used to stock document attribute values
47  global $is_array;
48  global $i;
49  global $depth_index;
50  global $title; //
51  global $attr_idoc; // used for idoc attribute (idoc and idoclist)
52  global $list; // used for list attribute (textlist and idoclist)
53  global $tempidoc; //is need to acces to $idoc in startElement()
54  $tempidoc = $idoc;
55  $list = array();
56  $depth_index = 0; //used for knowing the curent xml level. 0 at the begining
57  $i = 0;
58  $attr_idoc = false;
59  $value = "";
60  $tabvalues = array();
61  $is_array = false;
62 
63  $xml_parser = xml_parser_create("ISO-8859-1");
64  // use case-folding so we are sure to find the tag in $map_array
65  xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, true);
66  xml_set_element_handler($xml_parser, "startElementXml", "endElementXml");
67  xml_set_character_data_handler($xml_parser, "characterDataXml");
68 
69  if (!xml_parse($xml_parser, $fp)) {
70  die(sprintf("XML error: %s at line %d", xml_error_string(xml_get_error_code($xml_parser)) , xml_get_current_line_number($xml_parser)));
71  }
72  xml_parser_free($xml_parser);
73 
74  while ($attribut = each($tabvalues)) {
75  //printf($attribut[0]);
76  //printf(" : ");
77  //printf(sizeof($list[$attribut[0]]));
78  //printf("\n");
79  if (sizeof($list[$attribut[0]]) != 0) { //
80  //printf("tableau_sup_a_zero");
81  $value = "";
82  $ii = 0;
83  while ($x = each($list[$attribut[0]])) {
84  $ii++;
85  if (($ii) != 1) {
86  $value.= "\n";
87  }
88  $value.= $x[1];
89  }
90  //printf($value);
91  $idoc->SetValue($attribut[0], $value);
92  } else {
93  $idoc->SetValue($attribut[0], $attribut[1]);
94  }
95  }
96  $idoc->title = $title;
97 }
98 //these two next functions are used for idoc attributes
100 {
101  //printf("ici ");
102  $balise = "<$name ";
103  while (list($att, $valeur) = each($attrs)) {
104  $balise.= " $att=\"$valeur\"";
105  }
106  $balise.= ">";
107  //printf("balise_ouvrante");
108  return $balise;
109 }
110 
112 {
113  return "</$name>";
114 }
115 
116 function startElementXml($parser, $name, $attrs)
117 {
118  //this function is called when parser find a start element in the xml.
119  global $depth_index;
120  $depth_index++;
121  global $action;
122  global $title;
123  global $attr_idoc;
124  global $is_array;
125  global $value;
126  global $tempidoc;
127 
128  if ($depth_index == 1) {
129  $title = $attrs["TITLE"];
130  }
131 
132  if ($attr_idoc) {
133  $value.= recreate_balise_ouvrante($name, $attrs); //to recover xml of idoc or listidoc attribute.
134 
135  }
136 
137  if ($depth_index == 3) {
138 
139  $attribute = $tempidoc->GetAttribute($name);
140 
141  $is_array = false;
142  // $attr_idoc=false;
143  $is_array = $attribute->inArray();
144  if ($attribute->type == "idoc") {
145  $attr_idoc = true;
146  }
147  if ($attribute->repeat) {
148  $is_array = true;
149  }
150  }
151 }
152 
153 function endElement($parser, $name)
154 {
155  //this function is called when parser find a end element in the xml.
156  global $value;
157  global $tabvalues;
158  global $i;
159  global $list;
160  global $depth_index;
161  global $attr_idoc;
162  global $is_array;
163 
164  $value = trim($value);
165 
166  if ($depth_index == 3) {
167  if (!$is_array) { //case of single attribut
168  if ($attr_idoc) {
169  $tabvalues[$name] = base64_encode($value); // in case of idoc attribute, value(a xml) of the attribute is coded
170 
171  } else {
172  $tabvalues[$name] = $value;
173  }
174  } else {
175  $tabvalues[$name] = "something"; //the value is not important but $tabvalues[$name] must exist
176  //case of list attribut
177  if ($attr_idoc) {
178  $list[$name][$i] = base64_encode($value);
179  } else {
180  $list[$name][$i] = $value;
181  }
182  // printf("icic");
183  $i++;
184  }
185  $value = "";
186  if ($attr_idoc) {
187  $attr_idoc = false;
188  }
189  }
190 
191  if ($attr_idoc) { //to recover xml of idoc attribute
193  //printf("la ");printf($value);
194 
195  }
196 
197  $depth_index--;
198 }
199 
200 function characterDataXml($parser, $data)
201 {
202  global $value;
203  if (chop($data) != "") {
204  $value.= $data;
205  }
206 }
207 
208 function recup_argument_from_xml($xml, $nom_arg)
209 {
210 
211  $title = stristr($xml, "$nom_arg=");
212  $title = strstr($title, "\"");
213  $title = substr($title, 1);
214  $fin = strpos($title, "\"");
215  $title = substr($title, 0, $fin);
216  //printf($title);
217  return $title;
218 }
219 ?>
← centre documentaire © anakeen - published under CC License - Dynacase