1 | <?php
|
---|
2 |
|
---|
3 | class XMLTag
|
---|
4 | {
|
---|
5 | var $Name;
|
---|
6 | var $Attributes;
|
---|
7 | var $SubElements;
|
---|
8 | var $EndSymbol;
|
---|
9 | var $ShringEmpty;
|
---|
10 |
|
---|
11 | function __construct($Name = '')
|
---|
12 | {
|
---|
13 | $this->Name = $Name;
|
---|
14 | $this->Attributes = array();
|
---|
15 | $this->SubElements = array();
|
---|
16 | $this->EndSymbol = '/';
|
---|
17 | $this->ShringEmpty = true;
|
---|
18 | }
|
---|
19 |
|
---|
20 | function GetOutput()
|
---|
21 | {
|
---|
22 | $Content = '';
|
---|
23 | if(is_array($this->SubElements))
|
---|
24 | foreach($this->SubElements as $SubElement)
|
---|
25 | {
|
---|
26 | if(is_object($SubElement)) $Content .= $SubElement->GetOutput();
|
---|
27 | else $Content .= $SubElement;
|
---|
28 | } else $Content .= $this->SubElements;
|
---|
29 |
|
---|
30 | $AttributesText = '';
|
---|
31 | foreach($this->Attributes as $Name => $Value)
|
---|
32 | $AttributesText .= ' '.$Name.'="'.$Value.'"';
|
---|
33 |
|
---|
34 | if($this->Name != '')
|
---|
35 | {
|
---|
36 | if(($Content != '') or ($this->ShringEmpty == false)) $Output = '<'.$this->Name.$AttributesText.'>'.$Content.'<'.$this->EndSymbol.$this->Name.'>';
|
---|
37 | else $Output = '<'.$this->Name.$AttributesText.$this->EndSymbol.'>';
|
---|
38 | } else $Output = $Content;
|
---|
39 | return($Output);
|
---|
40 | }
|
---|
41 | }
|
---|
42 |
|
---|
43 | class XMLDocument
|
---|
44 | {
|
---|
45 | var $Formated;
|
---|
46 | var $Version;
|
---|
47 | var $Encoding;
|
---|
48 | var $Content;
|
---|
49 | var $Indentation;
|
---|
50 |
|
---|
51 | function __construct()
|
---|
52 | {
|
---|
53 | $this->Version = '1.0';
|
---|
54 | $this->Encoding = 'utf-8';
|
---|
55 | $this->Formated = false;
|
---|
56 | $this->Indentation = 2;
|
---|
57 | }
|
---|
58 |
|
---|
59 | function GetOutput()
|
---|
60 | {
|
---|
61 | $BaseTag = new XMLTag('?xml');
|
---|
62 | $BaseTag->Attributes = array('version' => $this->Version, 'encoding' => $this->Encoding);
|
---|
63 | $BaseTag->EndSymbol = '?';
|
---|
64 | $Output = $BaseTag->GetOutput();
|
---|
65 | $Output .= $this->Content->GetOutput();
|
---|
66 | if($this->Formated) $Output = $this->FormatOutput($Output);
|
---|
67 | return($Output);
|
---|
68 | }
|
---|
69 |
|
---|
70 | function FormatOutput($Text)
|
---|
71 | {
|
---|
72 | $Output = '';
|
---|
73 | $Indent = 0;
|
---|
74 | $IndentNew = 0;
|
---|
75 | while($Text != '')
|
---|
76 | {
|
---|
77 | $Start = strpos($Text, '<');
|
---|
78 | $End = strpos($Text, '>');
|
---|
79 | if($Start != 0)
|
---|
80 | {
|
---|
81 | $End = $Start - 1;
|
---|
82 | $Start = 0;
|
---|
83 | }
|
---|
84 | $Line = trim(substr($Text, $Start, $End + 1));
|
---|
85 | if(strlen($Line) > 0)
|
---|
86 | if($Line[0] == '<')
|
---|
87 | {
|
---|
88 | if($Text[$Start + 1] == '/')
|
---|
89 | {
|
---|
90 | $IndentNew = $IndentNew - $this->Indentation;
|
---|
91 | $Indent = $IndentNew;
|
---|
92 | } else
|
---|
93 | {
|
---|
94 | if(strpos($Line, ' ')) $Command = substr($Line, 1, strpos($Line, ' ') - 1);
|
---|
95 | else $Command = substr($Line, 1, strlen($Line) - $this->Indentation);
|
---|
96 | if(strpos($Text, '</'.$Command.'>')) $IndentNew = $IndentNew + $this->Indentation;
|
---|
97 | }
|
---|
98 | }
|
---|
99 | if($Line != '') $Output .= (str_repeat(' ', $Indent).$Line."\n");
|
---|
100 | $Text = substr($Text, $End + 1, strlen($Text));
|
---|
101 | $Indent = $IndentNew;
|
---|
102 | }
|
---|
103 | return($Output);
|
---|
104 | }
|
---|
105 | }
|
---|
106 |
|
---|
107 | ?>
|
---|