myDOMDocument (Een PHP5 DOM overload Class)

Gepost door Robbert Schellingerhout op 21-03-2009 22:41.

Een eenvoudige DOM overload class waarmee je xml files kan opslaan (method save) of kan echoen (method saveXML)

Let wel: alleen de benodigde DOM functies worden gebruikt, het is niet geschikt om html als waarde aan een element toe te kennen want dan heb je http://nl3.php.net/manual/en/domdocument.createcdatasection.php nodig.

Wat heb je nodig:
PHP5

Handig voor?:
- beantwoorden van requests (javascript: Ajax, e.d.)
- xml / xsl (http://www.php.net/xsl)
- maken van rss feed (http://www.rssboard.org/)
- maken van Excel xml-file

In de code zie je ook dat er gebruik gemaakt wordt van:
$this->{$element}

Mijn standunt hierin is in feite:
Gebruik de brackets niet om op deze manier variabelen aan te maken. (Vooral niet als deze voortkomen uit: $_POST / $_GET / $_REQUEST / $_SESSION / $_COOKIE)

Topics waarin andere voorbeelden te vinden zijn:
http://www.phpfreakz.nl/forum.php?forum=9&iid=1161827#id1162615
http://www.phpfreakz.nl/forum.php?forum=9&iid=1163424#id1163466

Excel class welke aansluit op de hier geposte class
http://www.phpfreakz.nl/forum.php?forum=6&iid=1219691#id1219720

Bestanden van dit script

index.php

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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
<?php
ini_set('display_errors',1); 
error_reporting(E_ALL); 
/**
    class: myDOMDocument 
    Een DOM overload Class 
*/
class myDOMDocument { 
    
    /**       
       Initialisatie van DOMDocument
       @param [string $version] 
       @param [string $encoding]
       @return void
       
       Voor meer uitleg zie:
       http://www.php.net/manual/en/domdocument.construct.php
    */
    public function __construct($version = '1.0', $encoding = 'iso-8859-1') { 
        $this->DOMDocument = new DOMDocument($version,$encoding); 
        $this->DOMDocument->preserveWhiteSpace = false; 
        $this->DOMDocument->formatOutput = true; 
    } 
    
    /**
        createProcessingInstruction
        @param string $name
        @param [array $attributes]
        @return void
        
        Voor meer uitleg zie:
        http://www.php.net/manual/en/domdocument.createprocessinginstruction.php  
    */
    public function createProcessingInstruction($name,$attributes = array()) {        
        static $att = ''; 
        if (sizeof($attributes)>0) { 
            
            foreach ($attributes as $key => $value) { 
                $att .= $key .'="'.$value.'"'; 
            } 
        } 
        
        $this->DOMDocument->appendChild($this->DOMDocument->createProcessingInstruction($name,$att)); 
    } 

    /**
        createElement
        @param string $name
        @param array $attributes
        @return void
        
        Voor meer uitleg zie:
        http://www.php.net/manual/en/domdocument.createelement.php
    */    
    public function createElement($element,$value ='') {        
        if ($value != '') { 
            $this->{$element} = $this->DOMDocument->createElement($element,$value);        
        } 
        else { 
            $this->{$element} = $this->DOMDocument->createElement($element);        
        } 
    } 
    
    /**
        setAttribute
        @param string $element
        @param string $name
        @param string $value
        @return void
        
        Voor meer uitleg zie:
        http://www.php.net/manual/en/domelement.setattribute.php
    */           
    public function setAttribute($element,$name,$value) { 
        $this->{$element}->setAttribute($name,$value); 
    } 

    /**
        appendChild
        @param string $root
        @param string $element
        @return void
                
        Voor meer uitleg zie:
        http://www.php.net/manual/en/domnode.appendchild.php
    */        
    public function appendChild($root,$element) { 
        
        switch (gettype($element)) { 
            case 'string': 
                $this->{$root}->appendChild($this->{$element}); 
                break; 
            default: 
                $this->{$root}->appendChild($element);                
                break; 
        } 
    } 
    
    /**
        saveXML
        @return string DOMDocument
                
        Voor meer uitleg zie:
        http://www.php.net/manual/en/domdocument.savexml.php
    */            
    public function saveXML() { 
        return $this->DOMDocument->saveXML(); 
    } 
    
    /**
        save
        @param string $path
        @param string $file
        @return string 
                
        Voor meer uitleg zie:
        http://www.php.net/manual/en/domdocument.save.php
    */                
    public function save($path,$file) { 
        return $this->DOMDocument->save($path.$file); 
    } 
    
    
    /**
        addElement
        @param string $root
        @param string $element
        @param [string $value]
        @package [array $attributes]
        @return void
        
        Een alles in 1 method, welke aangeroepen kan worden na het creeren van root element.
        
    */
    public function addElement($root,$element,$value ='',$attributes = array()) { 
        $this->createElement($element,$value); 
        
        if (sizeof($attributes)>0) { 
            foreach ($attributes as $name => $value) { 
                $this->setAttribute($element,$name,$value); 
            } 
        } 
        
        $this->appendChild($root,$this->{$element}); 
    } 
} 


$mysqli = new mysqli();
$playlistAttrubutes = array('version'=> 1,'xmlns'=> 'http://xspf.org/ns/0/');


// initialisatie myDOMDocument
// parameters version, encoding
$dom = new myDOMDocument('1.0','utf-8'); 

// creeren root element
$dom->createElement('playlist');

// zetten van attributen op het root element
foreach ($playlistAttrubutes as $key => $value) {
    $dom->setAttribute('playlist',$key,$value);
}
// toevoegen van root element aan DOMDocument
$dom->appendChild('DOMDocument','playlist'); 

// toevoegen van element trackList aan element playlist, zonder waarde en attributen
$dom->addElement('playlist','trackList'); 

$sql = "SELECT * FROM playlist"; 

if ($result = $mysqli->query($sql)) { 
    while ($row = $result->fetch_assoc()) {         
        $dom->addElement('trackList','track'); 
        $dom->addElement('track','title',$row['title']); 
        $dom->addElement('track','location',$row['location']); 
        $i++; 
    } 
} 

$dom->save('/www/htdocs/5/','tracks.xml'); 
?>

Resultaat van voorbeeld:
<?xml version="1.0" encoding="utf-8"?>
<playlist xmlns="http://xspf.org/ns/0/" version="1">
  <trackList>
    <track>
      <title>a</title>
      <location>a uit het alphabet</location>
    </track>
    <track>
      <title>b</title>
      <location>b uit het alphabet</location>
    </track>
    <track>
      <title>c</title>
      <location>c uit het alphabet</location>
    </track>
    <track>
      <title>d</title>
      <location>d uit het alphabet</location>
    </track>
    <track>
      <title>i</title>
      <location>i uit het alphabet</location>
    </track>
    <track>
      <title>f</title>
      <location>f uit het alphabet</location>
    </track>
    <track>
      <title>g</title>
      <location>g uit het alphabet</location>
    </track>
    <track>
      <title>h</title>
      <location>h uit het alphabet</location>
    </track>
    <track>
      <title>e</title>
      <location>e uit het alphabet</location>
    </track>
  </trackList>
</playlist>

Commentaar

24-08-2008 21:44

Vermeld hier alle zaken betreffende valkuilen, handige informatie, (installatie-)instructies e.d.

1
2
3
4
5
6
7
$this->DOMDocument = new DOMDocument($version,$encoding); 

Ik zie nergens een property DomDocument? Daarbij lijkt het mij nuttiger om DOMDocument
te extenden zodat je gelijk alle functies tot je beschikking hebt die je hier zelf aanmaakt.
Mij ontgaat het nut van deze class.

Wat voor voordeel heeft deze class ten opzichte van het normale DOMDocument object?
24-08-2008 22:29

Kijk nog eens goed naar de class en het voorbeeld.

Het voornaamste is dat als je eenmaal je root element gecreerd hebt aan DOMDocument je alleen nog maar de method addElement hoeft aan te roepen.

Meer dan dit heb je in principe niet nodig om xml files te genereren. (met hier en daar een uitzondering daargelaten, en dan doel ik voornamenlijk op createCDATASection)

Zie o.a. het kopje `Handig voor?`

Waarom niet extenden, wel om inzicht te houden in de manier waarop je de DOM objecten kan gebruiken. Bij extenden ga je enigzins verstoppertje spelen.

1
2
3
Post hier de source-code van je script. Alle informatie tussen <? ... ?> en <?php ... ?> zal automatisch worden getoond in color-coding. 

Let op! Het is niet de bedoeling om hier een link naar je website te plaatsen. Post hier gewoon de code, veel simpeler, sneller en meer kans dat het blijft staan.
Inloggen wachtwoord vergeten? Aanmelden