1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
xml.php
См. документацию.
1<?php
8
10
12{
13 var $name;
16 var $children = [];
20
21 public function name()
22 {
23 return $this->name;
24 }
25
26 public function children()
27 {
28 return $this->children;
29 }
30
31 public function textContent()
32 {
33 return $this->content;
34 }
35
36 public function getAttribute($attribute)
37 {
38 if (is_array($this->attributes))
39 {
40 foreach ($this->attributes as $anode)
41 {
42 if ($anode->name == $attribute)
43 {
44 return $anode->content;
45 }
46 }
47 }
48 return "";
49 }
50
51 public function getAttributes()
52 {
53 return $this->attributes;
54 }
55
56 public function namespaceURI()
57 {
58 return $this->getAttribute("xmlns");
59 }
60
65 public function elementsByName($tagname)
66 {
67 $result = array();
68
69 if ($this->name == $tagname)
70 {
71 $result[] = $this;
72 }
73
74 if (is_array($this->children))
75 {
76 foreach ($this->children as $node)
77 {
78 $more = $node->elementsByName($tagname);
79 if (is_array($more))
80 {
81 foreach ($more as $mnode)
82 {
83 $result[] = $mnode;
84 }
85 }
86 }
87 }
88 return $result;
89 }
90
92 {
93 if (isset($result[$name]))
94 {
95 $i = 1;
96 while (isset($result[$i.":".$name]))
97 $i++;
98 $result[$i.":".$name] = $value;
99 return "indexed";
100 }
101 else
102 {
103 $result[$name] = $value;
104 return "common";
105 }
106 }
107
108 function decodeDataTypes($attrAsNodeDecode = false)
109 {
110 $result = array();
111
112 if (!$this->children)
113 {
114 $this->_SaveDataType_OnDecode($result, $this->name(), $this->textContent());
115 }
116 else
117 {
118 foreach ($this->children() as $child)
119 {
120 $cheese = $child->children();
121 if (!$cheese or !count($cheese))
122 {
123 $this->_SaveDataType_OnDecode($result, $child->name(), $child->textContent());
124 }
125 else
126 {
127 $cheresult = $child->decodeDataTypes();
128 if (is_array($cheresult))
129 $this->_SaveDataType_OnDecode($result, $child->name(), $cheresult);
130 }
131 }
132 }
133
134 if ($attrAsNodeDecode)
135 {
136 foreach ($this->getAttributes() as $child)
137 {
138 $this->_SaveDataType_OnDecode($result, $child->name(), $child->textContent());
139 }
140 }
141
142 return $result;
143 }
144
145 function __toString()
146 {
147 switch ($this->name)
148 {
149 case "cdata-section":
150 $ret = "<![CDATA[";
151 $ret .= $this->content;
152 $ret .= "]]>";
153 break;
154
155 default:
156 $isOneLiner = false;
157
158 if (empty($this->children) && $this->content == '')
159 $isOneLiner = true;
160
161 $attrStr = "";
162
163 if (is_array($this->attributes))
164 {
165 foreach ($this->attributes as $attr)
166 {
167 $attrStr .= " ".$attr->name."=\"".CDataXML::xmlspecialchars($attr->content)."\" ";
168 }
169 }
170
171 if ($isOneLiner)
172 $oneLinerEnd = " /";
173 else
174 $oneLinerEnd = "";
175
176 $ret = "<".$this->name.$attrStr.$oneLinerEnd.">";
177
178 if (is_array($this->children))
179 {
180 foreach ($this->children as $child)
181 {
182 $ret .= $child->__toString();
183 }
184 }
185
186 if (!$isOneLiner)
187 {
188 if ($this->content <> '')
189 $ret .= CDataXML::xmlspecialchars($this->content);
190
191 $ret .= "</".$this->name.">";
192 }
193
194 break;
195 }
196
197 return $ret;
198 }
199
200 public function __toArray()
201 {
202 $retHash = array(
203 "@" => array(),
204 );
205
206 if (is_array($this->attributes))
207 {
208 foreach ($this->attributes as $attr)
209 {
210 $retHash["@"][$attr->name] = $attr->content;
211 }
212 }
213
214 if ($this->content != "")
215 {
216 $retHash["#"] = $this->content;
217 }
218 elseif (!empty($this->children))
219 {
220 $ar = array();
221 foreach ($this->children as $child)
222 {
223 $ar[$child->name][] = $child->__toArray();
224 }
225 $retHash["#"] = $ar;
226 }
227 else
228 {
229 $retHash["#"] = "";
230 }
231
232 return $retHash;
233 }
234
235 public function toSimpleArray()
236 {
237 if (!empty($this->children))
238 {
239 $ar = [];
240 foreach ($this->children as $child)
241 {
242 $ar[$child->name][] = $child->toSimpleArray();
243 }
244
245 // simplify the array
246 foreach ($ar as $name => $value)
247 {
248 if (count($value) == 1)
249 {
250 $ar[$name] = $value[0];
251 }
252 }
253 return $ar;
254 }
255
256 return $this->content;
257 }
258}
259
261{
262 var $version = '';
263 var $encoding = '';
264
267 var $root;
268
269 function elementsByName($tagname)
270 {
271 $result = array();
272 if (is_array($this->children))
273 {
274 foreach ($this->children as $node)
275 {
276 $more = $node->elementsByName($tagname);
277 if (is_array($more))
278 {
279 foreach ($more as $mnode)
280 {
281 $result[] = $mnode;
282 }
283 }
284 }
285 }
286 return $result;
287 }
288
289 public static function encodeDataTypes($name, $value)
290 {
291 static $Xsd = array(
292 "string"=>"string", "bool"=>"boolean", "boolean"=>"boolean",
293 "int"=>"integer", "integer"=>"integer", "double"=>"double", "float"=>"float", "number"=>"float",
294 "array"=>"anyType", "resource"=>"anyType",
295 "mixed"=>"anyType", "unknown_type"=>"anyType", "anyType"=>"anyType"
296 );
297
298 $node = new CDataXMLNode();
299 $node->name = $name;
300
301 if (is_object($value))
302 {
303 $ovars = get_object_vars($value);
304 foreach ($ovars as $pn => $pv)
305 {
306 $decode = static::encodeDataTypes($pn, $pv);
307 if ($decode)
308 {
309 $node->children[] = $decode;
310 }
311 }
312 }
313 else if (is_array($value))
314 {
315 foreach ($value as $pn => $pv)
316 {
317 $decode = static::encodeDataTypes( $pn, $pv);
318 if ($decode)
319 {
320 $node->children[] = $decode;
321 }
322 }
323 }
324 else
325 {
326 if (isset($Xsd[gettype($value)]))
327 {
328 $node->content = $value;
329 }
330 }
331 return $node;
332 }
333
334 /* Returns an XML string of the DOM document */
335 public function __toString()
336 {
337 $ret = "<"."?xml";
338 if ($this->version <> '')
339 {
340 $ret .= " version=\"".$this->version."\"";
341 }
342 if ($this->encoding <> '')
343 {
344 $ret .= " encoding=\"".$this->encoding."\"";
345 }
346 $ret .= "?".">";
347
348 if (is_array($this->children))
349 {
350 foreach ($this->children as $child)
351 {
352 $ret .= $child->__toString();
353 }
354 }
355
356 return $ret;
357 }
358
359 /* Returns an array of the DOM document */
360 public function __toArray()
361 {
362 $arRetArray = array();
363
364 if (is_array($this->children))
365 {
366 foreach ($this->children as $child)
367 {
368 $arRetArray[$child->name] = $child->__toArray();
369 }
370 }
371
372 return $arRetArray;
373 }
374
379 public function toSimpleArray(): array
380 {
381 $arRetArray = [];
382
383 if (is_array($this->children))
384 {
385 foreach ($this->children as $child)
386 {
387 $arRetArray[$child->name] = $child->toSimpleArray();
388 }
389 }
390
391 return $arRetArray;
392 }
393}
394
396{
398 var $tree;
400
401 var $delete_ns = true;
402
403 public function __construct($TrimWhiteSpace = true)
404 {
405 $this->TrimWhiteSpace = (bool)$TrimWhiteSpace;
406 $this->tree = false;
407 }
408
409 public function Load($file)
410 {
411 $this->tree = false;
412
413 if (file_exists($file))
414 {
415 $content = file_get_contents($file);
416
417 if (defined('SITE_CHARSET'))
418 {
419 $charset = (defined("BX_DEFAULT_CHARSET")? BX_DEFAULT_CHARSET : "windows-1251");
420 if (preg_match("/<"."\\?XML[^>]+encoding=[\"']([^>\"']+)[\"'][^>]*\\?".">/i", $content, $matches))
421 {
422 $charset = trim($matches[1]);
423 }
424 $content = Encoding::convertEncoding($content, $charset, SITE_CHARSET);
425 }
426 $this->tree = $this->__parse($content);
427 return $this->tree !== false;
428 }
429
430 return false;
431 }
432
433 public function LoadString($text)
434 {
435 $this->tree = false;
436
437 if ($text <> '')
438 {
439 $this->tree = $this->__parse($text);
440 return ($this->tree !== false);
441 }
442
443 return false;
444 }
445
446 public function GetTree()
447 {
448 return $this->tree;
449 }
450
451 public function GetArray()
452 {
453 if (!is_object($this->tree))
454 {
455 return false;
456 }
457 return $this->tree->__toArray();
458 }
459
460 public function GetString()
461 {
462 if (!is_object($this->tree))
463 {
464 return false;
465 }
466 return $this->tree->__toString();
467 }
468
472 public function SelectNodes($strNode)
473 {
474 if (!is_object($this->tree))
475 {
476 return false;
477 }
478
480
481 $tmp = explode("/", $strNode);
482 $tmpCount = count($tmp);
483 for ($i = 1; $i < $tmpCount; $i++)
484 {
485 if ($tmp[$i] != "")
486 {
487 if (!is_array($result->children))
488 return false;
489
490 $bFound = false;
491 for ($j = 0, $c = count($result->children); $j < $c; $j++)
492 {
493 if ($result->children[$j]->name==$tmp[$i])
494 {
495 $result = $result->children[$j];
496 $bFound = true;
497 break;
498 }
499 }
500
501 if (!$bFound)
502 {
503 return false;
504 }
505 }
506 }
507
508 return $result;
509 }
510
511 public static function xmlspecialchars($str)
512 {
513 static $search = array("&","<",">","\"","'");
514 static $replace = array("&amp;","&lt;","&gt;","&quot;","&apos;");
515 return str_replace($search, $replace, $str);
516 }
517
518 public static function xmlspecialcharsback($str)
519 {
520 static $search = array("&lt;","&gt;","&quot;","&apos;","&amp;");
521 static $replace = array("<",">","\"","'","&");
522 return str_replace($search, $replace, $str);
523 }
524
531 protected function __parse(&$strXMLText)
532 {
533 static $search = array("&gt;","&lt;","&apos;","&quot;","&amp;");
534 static $replace = array(">","<","'",'"',"&");
535
536 $oXMLDocument = new CDataXMLDocument();
537
538 // strip comments
539 $strXMLText = CDataXML::__stripComments($strXMLText);
540
541 // stip the !doctype
542 // The DOCTYPE declaration can consist of an internal DTD in square brackets
543 $cnt = 0;
544 $strXMLText = preg_replace("%<!DOCTYPE[^\\[>]*\\[.*?]>%is", "", $strXMLText, -1, $cnt);
545 if ($cnt == 0)
546 {
547 $strXMLText = preg_replace("%<!DOCTYPE[^>]*>%i", "", $strXMLText);
548 }
549
550 // get document version and encoding from header
551 preg_match_all("#<\\?(.*?)\\?>#i", $strXMLText, $arXMLHeader_tmp);
552 foreach ($arXMLHeader_tmp[0] as $strXMLHeader_tmp)
553 {
554 preg_match_all("/([a-zA-Z:]+=\".*?\")/i", $strXMLHeader_tmp, $arXMLParam_tmp);
555 foreach ($arXMLParam_tmp[0] as $strXMLParam_tmp)
556 {
557 if ($strXMLParam_tmp <> '')
558 {
559 $arXMLAttribute_tmp = explode("=\"", $strXMLParam_tmp);
560 if ($arXMLAttribute_tmp[0] == "version")
561 $oXMLDocument->version = mb_substr($arXMLAttribute_tmp[1], 0, mb_strlen($arXMLAttribute_tmp[1]) - 1);
562 elseif ($arXMLAttribute_tmp[0] == "encoding")
563 $oXMLDocument->encoding = mb_substr($arXMLAttribute_tmp[1], 0, mb_strlen($arXMLAttribute_tmp[1]) - 1);
564 }
565 }
566 }
567
568 // strip header
569 $strXMLText = preg_replace("#<\\?.*?\\?>#", "", $strXMLText);
570
571 $oXMLDocument->root = &$oXMLDocument->children;
572
574 $currentNode = &$oXMLDocument;
575
576 $tok = strtok($strXMLText, "<");
577 $arTag = explode(">", $tok);
578 if (count($arTag) < 2)
579 {
580 //There was whitespace before <, so make another try
581 $tok = strtok("<");
582 $arTag = explode(">", $tok);
583 if (count($arTag) < 2)
584 {
585 //It's a broken XML
586 return false;
587 }
588 }
589
590 while ($tok !== false)
591 {
592 $tagName = $arTag[0];
593 $tagContent = $arTag[1];
594
595 // find tag name with attributes
596 // check if it's an endtag </tagname>
597 if ($tagName[0] == "/")
598 {
599 $tagName = mb_substr($tagName, 1);
600 // strip out namespace; nameSpace:Name
601 if ($this->delete_ns)
602 {
603 $colonPos = mb_strpos($tagName, ":");
604
605 if ($colonPos > 0)
606 $tagName = mb_substr($tagName, $colonPos + 1);
607 }
608
609 if ($currentNode->name != $tagName)
610 {
611 // Error parsing XML, unmatched tags $tagName
612 return false;
613 }
614
615 $currentNode = $currentNode->_parent;
616
617 // convert special chars
618 if ((!$this->TrimWhiteSpace) || (trim($tagContent) != ""))
619 $currentNode->content = str_replace($search, $replace, $tagContent);
620 }
621 elseif (strncmp($tagName, "![CDATA[", 8) === 0)
622 {
623 //because cdata may contain > and < chars
624 //it is special processing needed
625 $cdata = "";
626 for($i = 0, $c = count($arTag); $i < $c; $i++)
627 {
628 $cdata .= $arTag[$i].">";
629 if (str_ends_with($cdata, "]]>"))
630 {
631 $tagContent = $arTag[$i+1];
632 break;
633 }
634 }
635
636 if (!str_ends_with($cdata, "]]>"))
637 {
638 $cdata = mb_substr($cdata, 0, -1)."<";
639 do
640 {
641 $tok = strtok(">");//unfortunatly strtok eats > followed by >
642 $cdata .= $tok.">";
643 //util end of string or end of cdata found
644 }
645 while ($tok !== false && !str_ends_with($tok, "]]"));
646 //$tagName = substr($tagName, 0, -1);
647 }
648
649 $cdataSection = mb_substr($cdata, 8, -3);
650
651 // new CDATA node
652 $subNode = new CDataXMLNode();
653 $subNode->name = "cdata-section";
654 $subNode->content = $cdataSection;
655
656 $currentNode->children[] = $subNode;
657 $currentNode->content .= $subNode->content;
658
659 // convert special chars
660 if ((!$this->TrimWhiteSpace) || (trim($tagContent) != ""))
661 $currentNode->content = str_replace($search, $replace, $tagContent);
662 }
663 else
664 {
665 // normal start tag
666 if (preg_match('/^(\S+)(.*)$/s', $tagName, $match))
667 {
668 $justName = $match[1];
669 $attributePart = $match[2];
670 }
671 else
672 {
673 $justName = $tagName;
674 $attributePart = '';
675 }
676
677 // strip out namespace; nameSpace:Name
678 if ($this->delete_ns)
679 {
680 $colonPos = mb_strpos($justName, ":");
681
682 if ($colonPos > 0)
683 $justName = mb_substr($justName, $colonPos + 1);
684 }
685
686 // remove trailing / from the name if exists
687 $justName = rtrim($justName, "/");
688
689 $subNode = new CDataXMLNode();
690 $subNode->_parent = $currentNode;
691 $subNode->name = $justName;
692
693 // find attributes
694 if ($attributePart)
695 {
696 // attributes
697 $attr = $this->__parseAttributes($attributePart);
698
699 if ($attr)
700 {
701 $subNode->attributes = $attr;
702 }
703 }
704
705 // convert special chars
706 if ((!$this->TrimWhiteSpace) || (trim($tagContent) != ""))
707 {
708 $subNode->content = str_replace($search, $replace, $tagContent);
709 }
710
711 $currentNode->children[] = $subNode;
712
713 if (!str_ends_with($tagName, "/"))
714 {
715 $currentNode = $subNode;
716 }
717 }
718
719 //Next iteration
720 $tok = strtok("<");
721 $arTag = explode(">", $tok);
722 //There was whitespace before < just after CDATA section, so make another try
723 if (count($arTag) < 2 && (strncmp($tagName, "![CDATA[", 8) === 0))
724 {
725 $currentNode->content .= $arTag[0];
726
727 // convert special chars
728 if ((!$this->TrimWhiteSpace) || (trim($tagContent) != ""))
729 $currentNode->content = str_replace($search, $replace, $tagContent);
730
731 $tok = strtok("<");
732 $arTag = explode(">", $tok);
733 }
734 }
735 return $oXMLDocument;
736 }
737
738 protected static function __stripComments($str)
739 {
740 $str = preg_replace("#<!--.*?-->#s", "", $str);
741 return $str;
742 }
743
744 /* Parses the attributes. Returns false if no attributes in the supplied string is found */
745 protected function __parseAttributes($attributeString)
746 {
747 $ret = false;
748
749 preg_match_all("/(\\S+)\\s*=\\s*([\"'])(.*?)\\2/su", $attributeString, $attributeArray);
750
751 foreach ($attributeArray[0] as $i => $attributePart)
752 {
753 $attributePart = trim($attributePart);
754 if ($attributePart != "" && $attributePart != "/")
755 {
756 $attributeName = $attributeArray[1][$i];
757
758 // strip out namespace; nameSpace:Name
759 if ($this->delete_ns)
760 {
761 $colonPos = mb_strpos($attributeName, ":");
762
763 if ($colonPos > 0)
764 {
765 // exclusion: xmlns attribute is xmlns:nameSpace
766 if ($colonPos == 5 && (mb_substr($attributeName, 0, $colonPos) == 'xmlns'))
767 {
768 $attributeName = 'xmlns';
769 }
770 else
771 {
772 $attributeName = mb_substr($attributeName, $colonPos + 1);
773 }
774 }
775 }
776 $attributeValue = $attributeArray[3][$i];
777
778 $attrNode = new CDataXMLNode();
779 $attrNode->name = $attributeName;
780 $attrNode->content = CDataXML::xmlspecialcharsback($attributeValue);
781
782 $ret[] = $attrNode;
783 }
784 }
785 return $ret;
786 }
787}
788/*
789Usage:
790
791class OrderLoader
792{
793 var $errors = array();
794
795 function elementHandler($path, $attr)
796 {
797 AddMessage2Log(print_r(array($path, $attr), true));
798 }
799
800 function nodeHandler(CDataXML $xmlObject)
801 {
802 AddMessage2Log(print_r($xmlObject, true));
803 }
804}
805
806$position = false;
807$loader = new OrderLoader;
808
809while(true) //this while is cross hit emulation
810{
811 $o = new CXMLFileStream;
812 $o->registerElementHandler("/КоммерческаяИнформация", array($loader, "elementHandler"));
813 $o->registerNodeHandler("/КоммерческаяИнформация/Каталог/Товары/Товар", array($loader, "nodeHandler"));
814 $o->setPosition($position);
815
816 if ($o->openFile($_SERVER["DOCUMENT_ROOT"]."/upload/081_books_books-books_ru.xml"))
817 {
818 while($o->findNext())
819 {
820 //if (time() > $endTime)
821 break;
822 }
823
824 if ($o->endOfFile())
825 {
826 break;
827 }
828 else
829 {
830 $position = $o->getPosition();
831 }
832 }
833}
834*/
836{
837 private $fileCharset = false;
838 private $filePosition = 0;
839 private $xmlPosition = "";
840 private $nodeHandlers = array();
841 private $elementHandlers = array();
842 private $endNodes = array();
843 private $fileHandler = null;
844
845 private $eof = false;
846 private $readSize = 1024;
847 private $buf = "";
848 private $bufPosition = 0;
849 private $bufLen = 0;
850 private $positionStack = array();
851 private $elementStack = array();
860 public function registerNodeHandler($nodePath, $callableHandler)
861 {
862 if (is_callable($callableHandler))
863 {
864 if (!isset($this->nodeHandlers[$nodePath]))
865 $this->nodeHandlers[$nodePath] = array();
866 $this->nodeHandlers[$nodePath][] = $callableHandler;
867
868 $pathComponents = explode("/", $nodePath);
869 $this->endNodes[end($pathComponents)] = true;
870 }
871 }
872
880 public function registerElementHandler($nodePath, $callableHandler)
881 {
882 if (is_callable($callableHandler))
883 {
884 if (!isset($this->elementHandlers[$nodePath]))
885 $this->elementHandlers[$nodePath] = array();
886 $this->elementHandlers[$nodePath][] = $callableHandler;
887
888 $pathComponents = explode("/", $nodePath);
889 $this->endNodes[end($pathComponents)] = true;
890 }
891 }
892
899 public function openFile($filePath)
900 {
901 $this->fileHandler = null;
902
903 $io = CBXVirtualIo::getInstance();
904 $file = $io->getFile($filePath);
905 $this->fileHandler = $file->open("rb");
906 if (is_resource($this->fileHandler))
907 {
908 if ($this->filePosition > 0)
909 fseek($this->fileHandler, $this->filePosition);
910
911 $this->elementStack = array();
912 $this->positionStack = array();
913 foreach (explode("/", $this->xmlPosition) as $pathPart)
914 {
915 @list($elementPosition, $elementName) = explode("@", $pathPart, 2);
916 $this->elementStack[] = $elementName;
917 $this->positionStack[] = $elementPosition;
918 }
919
920 return true;
921 }
922 else
923 {
924 return false;
925 }
926 }
927
933 public function endOfFile()
934 {
935 if ($this->fileHandler === null)
936 return true;
937 else
938 return $this->eof;
939 }
940
946 public function getPosition()
947 {
948 $this->xmlPosition = array();
949 foreach ($this->elementStack as $i => $elementName)
950 {
951 $this->xmlPosition[] = $this->positionStack[$i]."@".$elementName;
952 }
953 $this->xmlPosition = implode("/", $this->xmlPosition);
954
955 return array(
956 $this->fileCharset,
957 $this->filePosition,
958 $this->xmlPosition,
959 );
960 }
961
968 public function setPosition($position)
969 {
970 if (is_array($position))
971 {
972 if (isset($position[0]))
973 $this->fileCharset = $position[0];
974 if (isset($position[1]))
975 $this->filePosition = $position[1];
976 if (isset($position[2]))
977 $this->xmlPosition = $position[2];
978 }
979 }
980
986 public function findNext()
987 {
988 $cs = $this->fileCharset;
989
990 if ($this->fileHandler === null)
991 return false;
992
993 $this->eof = false;
994 while (($xmlChunk = $this->getXmlChunk()) !== false)
995 {
996 $origChunk = $xmlChunk;
997 if ($cs)
998 {
999 $xmlChunk = Encoding::convertEncoding($origChunk, $cs, LANG_CHARSET);
1000 }
1001
1002 if ($xmlChunk[0] == "/")
1003 {
1004 $this->endElement();
1005 return true;
1006 }
1007 elseif ($xmlChunk[0] == "!" || $xmlChunk[0] == "?")
1008 {
1009 if (str_starts_with($xmlChunk, "?xml"))
1010 {
1011 if (preg_match('#encoding\s*=\s*"(.*?)"#i', $xmlChunk, $arMatch))
1012 {
1013 $this->fileCharset = $arMatch[1];
1014 if (mb_strtoupper($this->fileCharset) === mb_strtoupper(LANG_CHARSET))
1015 {
1016 $this->fileCharset = false;
1017 }
1018 $cs = $this->fileCharset;
1019 }
1020 }
1021 elseif (str_starts_with($xmlChunk, '![CDATA['))
1022 {
1023 while (!str_ends_with($xmlChunk, ']]>'))
1024 {
1025 $xmlChunk = $this->getXmlChunk();
1026 if ($xmlChunk === false)
1027 {
1028 break;
1029 }
1030 }
1031 }
1032 }
1033 else
1034 {
1035 $this->startElement($xmlChunk, $origChunk);
1036 //check for self-closing tag
1037 $p = mb_strpos($xmlChunk, ">");
1038 if (($p !== false) && (mb_substr($xmlChunk, $p - 1, 1) == "/"))
1039 {
1040 $this->endElement();
1041 return true;
1042 }
1043 }
1044 }
1045 $this->eof = true;
1046
1047 return false;
1048 }
1049
1055 private function getXmlChunk()
1056 {
1057 if ($this->bufPosition >= $this->bufLen)
1058 {
1059 if (is_resource($this->fileHandler) && !feof($this->fileHandler))
1060 {
1061 $this->buf = fread($this->fileHandler, $this->readSize);
1062 $this->bufPosition = 0;
1063 $this->bufLen = strlen($this->buf);
1064 }
1065 else
1066 {
1067 return false;
1068 }
1069 }
1070
1071 //Skip line delimiters (ltrim)
1072 $xml_position = strpos($this->buf, "<", $this->bufPosition);
1073 while ($xml_position === $this->bufPosition)
1074 {
1075 $this->bufPosition++;
1076 $this->filePosition++;
1077 //Buffer ended with white space so we can refill it
1078 if ($this->bufPosition >= $this->bufLen)
1079 {
1080 if (!feof($this->fileHandler))
1081 {
1082 $this->buf = fread($this->fileHandler, $this->readSize);
1083 $this->bufPosition = 0;
1084 $this->bufLen = strlen($this->buf);
1085 }
1086 else
1087 {
1088 return false;
1089 }
1090 }
1091 $xml_position = strpos($this->buf, "<", $this->bufPosition);
1092 }
1093
1094 //Let's find next line delimiter
1095 while ($xml_position===false)
1096 {
1097 $next_search = $this->bufLen;
1098 //Delimiter not in buffer so try to add more data to it
1099 if (!feof($this->fileHandler))
1100 {
1101 $this->buf .= fread($this->fileHandler, $this->readSize);
1102 $this->bufLen = strlen($this->buf);
1103 }
1104 else
1105 {
1106 break;
1107 }
1108
1109 //Let's find xml tag start
1110 $xml_position = strpos($this->buf, "<", $next_search);
1111 }
1112 if ($xml_position===false)
1113 {
1114 $xml_position = $this->bufLen+1;
1115 }
1116
1117 $len = $xml_position-$this->bufPosition;
1118 $this->filePosition += $len;
1119 $result = substr($this->buf, $this->bufPosition, $len);
1120 $this->bufPosition = $xml_position;
1121
1122 return $result;
1123 }
1132 private function startElement($xmlChunk, $origChunk)
1133 {
1134 static $search = array(
1135 "'&(quot|#34);'i",
1136 "'&(lt|#60);'i",
1137 "'&(gt|#62);'i",
1138 "'&(amp|#38);'i",
1139 );
1140
1141 static $replace = array(
1142 "\"",
1143 "<",
1144 ">",
1145 "&",
1146 );
1147
1148 $p = mb_strpos($xmlChunk, ">");
1149 if ($p !== false)
1150 {
1151 if (mb_substr($xmlChunk, $p - 1, 1) == "/")
1152 {
1153 $elementName = mb_substr($xmlChunk, 0, $p - 1);
1154 }
1155 else
1156 {
1157 $elementName = mb_substr($xmlChunk, 0, $p);
1158 }
1159
1160 if (($ps = mb_strpos($elementName, " "))!==false)
1161 {
1162 $elementAttrs = mb_substr($elementName, $ps + 1);
1163 $elementName = mb_substr($elementName, 0, $ps);
1164 }
1165 else
1166 {
1167 $elementAttrs = "";
1168 }
1169
1170 $this->elementStack[] = $elementName;
1171 $this->positionStack[] = $this->filePosition - strlen($origChunk) - 1;
1172
1173 if (isset($this->endNodes[$elementName]))
1174 {
1175 $xmlPath = implode("/", $this->elementStack);
1176 if (isset($this->elementHandlers[$xmlPath]))
1177 {
1178 $attributes = array();
1179 if ($elementAttrs !== "")
1180 {
1181 preg_match_all("/(\\S+)\\s*=\\s*\"(.*?)\"/s", $elementAttrs, $attrs_tmp);
1182 if (!str_contains($elementAttrs, "&"))
1183 {
1184 foreach ($attrs_tmp[1] as $i=>$attrs_tmp_1)
1185 {
1186 $attributes[$attrs_tmp_1] = $attrs_tmp[2][$i];
1187 }
1188 }
1189 else
1190 {
1191 foreach ($attrs_tmp[1] as $i=>$attrs_tmp_1)
1192 {
1193 $attributes[$attrs_tmp_1] = preg_replace($search, $replace, $attrs_tmp[2][$i]);
1194 }
1195 }
1196 }
1197
1198 foreach ($this->elementHandlers[$xmlPath] as $callableHandler)
1199 {
1200 call_user_func_array($callableHandler, array(
1201 $xmlPath,
1202 $attributes,
1203 ));
1204 }
1205 }
1206 }
1207 }
1208 }
1215 private function endElement()
1216 {
1217 $elementName = array_pop($this->elementStack);
1218 $elementPosition = array_pop($this->positionStack);
1219
1220 if (isset($this->endNodes[$elementName]))
1221 {
1222 $xmlPath = implode("/", $this->elementStack)."/".$elementName;
1223 if (isset($this->nodeHandlers[$xmlPath]))
1224 {
1225 $xmlObject = $this->readXml($elementPosition, $this->filePosition);
1226 if (is_object($xmlObject))
1227 {
1228 foreach ($this->nodeHandlers[$xmlPath] as $callableHandler)
1229 {
1230 call_user_func_array($callableHandler, array(
1231 $xmlObject,
1232 ));
1233 }
1234 }
1235 }
1236 }
1237 }
1245 private function readXml($startPosition, $endPosition)
1246 {
1247 $xmlChunk = $this->readFilePart($startPosition, $endPosition);
1248 if ($xmlChunk && $this->fileCharset)
1249 {
1250 $xmlChunk = Encoding::convertEncoding($xmlChunk, $this->fileCharset, LANG_CHARSET);
1251 }
1252
1253 $xmlObject = new CDataXML;
1254 if ($xmlObject->loadString($xmlChunk))
1255 {
1256 return $xmlObject;
1257 }
1258 return false;
1259 }
1260
1268 public function readFilePart($startPosition, $endPosition)
1269 {
1270 if (is_resource($this->fileHandler))
1271 {
1272 $savedPosition = ftell($this->fileHandler);
1273 fseek($this->fileHandler, $startPosition);
1274 $xmlChunk = fread($this->fileHandler, $endPosition - $startPosition);
1275 fseek($this->fileHandler, $savedPosition);
1276 return $xmlChunk;
1277 }
1278 return false;
1279 }
1280}
xml version
Определения yandex.php:67
Определения xml.php:261
$version
Определения xml.php:262
static encodeDataTypes($name, $value)
Определения xml.php:289
__toString()
Определения xml.php:335
elementsByName($tagname)
Определения xml.php:269
__toArray()
Определения xml.php:360
toSimpleArray()
Определения xml.php:379
$children
Определения xml.php:266
$root
Определения xml.php:267
$encoding
Определения xml.php:263
Определения xml.php:396
GetTree()
Определения xml.php:446
Load($file)
Определения xml.php:409
__parseAttributes($attributeString)
Определения xml.php:745
GetArray()
Определения xml.php:451
static xmlspecialcharsback($str)
Определения xml.php:518
$tree
Определения xml.php:398
LoadString($text)
Определения xml.php:433
__construct($TrimWhiteSpace=true)
Определения xml.php:403
static __stripComments($str)
Определения xml.php:738
SelectNodes($strNode)
Определения xml.php:472
GetString()
Определения xml.php:460
$delete_ns
Определения xml.php:401
static xmlspecialchars($str)
Определения xml.php:511
$TrimWhiteSpace
Определения xml.php:399
Определения xml.php:12
name()
Определения xml.php:21
$content
Определения xml.php:14
_SaveDataType_OnDecode(&$result, $name, $value)
Определения xml.php:91
textContent()
Определения xml.php:31
__toString()
Определения xml.php:145
elementsByName($tagname)
Определения xml.php:65
__toArray()
Определения xml.php:200
$_parent
Определения xml.php:19
children()
Определения xml.php:26
toSimpleArray()
Определения xml.php:235
$children
Определения xml.php:16
$name
Определения xml.php:13
namespaceURI()
Определения xml.php:56
getAttribute($attribute)
Определения xml.php:36
$attributes
Определения xml.php:18
decodeDataTypes($attrAsNodeDecode=false)
Определения xml.php:108
getAttributes()
Определения xml.php:51
Определения xml.php:836
setPosition($position)
Определения xml.php:968
getPosition()
Определения xml.php:946
findNext()
Определения xml.php:986
endOfFile()
Определения xml.php:933
openFile($filePath)
Определения xml.php:899
registerNodeHandler($nodePath, $callableHandler)
Определения xml.php:860
readFilePart($startPosition, $endPosition)
Определения xml.php:1268
registerElementHandler($nodePath, $callableHandler)
Определения xml.php:880
$str
Определения commerceml2.php:63
$content
Определения commerceml.php:144
if(!is_array($prop["VALUES"])) $tmp
Определения component_props.php:203
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$result
Определения get_property_values.php:14
$bFound
Определения get_search.php:40
$p
Определения group_list_element_edit.php:23
if( $adminSidePanelHelper->isSidePanel())
Определения csv_new_setup.php:231
$io
Определения csv_new_run.php:98
const SITE_CHARSET
Определения include.php:62
const LANG_CHARSET
Определения include.php:65
$name
Определения menu_edit.php:35
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)
Определения prolog_main_admin.php:393
$ar
Определения options.php:199
$text
Определения template_pdf.php:79
$i
Определения factura.php:643
</p ></td >< td valign=top style='border-top:none;border-left:none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 2.0pt 0cm 2.0pt;height:9.0pt'>< p class=Normal align=center style='margin:0cm;margin-bottom:.0001pt;text-align:center;line-height:normal'>< a name=ТекстовоеПоле54 ></a ><?=($taxRate > count( $arTaxList) > 0) ? $taxRate."%"
Определения waybill.php:936
$matches
Определения index.php:22