Home > Article > Backend Development > Use DomIt! to do simple XML processing_PHP tutorial
One time I got a job processing XML files. This XML file is used to communicate with the Flash mp3 player and contains the address, description, etc. of the music. All I need is a form that allows an administrator to add, remove music items, and update the XML file. Sounds simple, right? But I'm having trouble with the delete function. When I spent countless hours scratching my head, searching, and reading about DOM XML in the official PHP documentation, DomIt! appeared in time (in fact, I found it in time). DomIt! is very powerful, easy to use (at least for simple processing), compatible with PHP4, and helps you learn DOM XML quickly if you are willing to peruse its source code.
The XML file required at that time is in the following format:
<span id="ubkp4" style="color: rgb(0,153,0)"><span id="ubkp5" style="color: black"><b id="bw6l"><</b></span>?xml<span id="ubkp6" style="color: rgb(0,0,102)">version</span>=<span id="ubkp7" style="color: rgb(255,0,0)">"1.0"</span><span id="ubkp8" style="color: black"><b id="bw6l0">?></b></span></span><br id="ubkp9" /><span id="ubkp10" style="color: rgb(0,153,0)"><span id="ubkp11" style="color: black"><b id="bw6l1"><audio</b><span id="ubkp12" style="color: black"><b id="bw6l2">></b></span></span></span><br id="ubkp13" /> <span id="ubkp14" style="color: rgb(0,153,0)"><span id="ubkp15" style="color: black"><b id="bw6l3"><file</b><span id="ubkp16" style="color: black"><b id="bw6l4">></b></span></span></span><br id="ubkp17" /> <span id="ubkp18" style="color: rgb(0,153,0)"><span id="ubkp19" style="color: black"><b id="bw6l5"><track</b><span id="ubkp20" style="color: black"><b id="bw6l6">></b></span></span></span>音乐文件位置<span id="ubkp21" style="color: rgb(0,153,0)"><span id="ubkp22" style="color: black"><b id="bw6l7"></track</b><span id="ubkp23" style="color: black"><b id="bw6l8">></b></span></span></span><br id="ubkp24" /> <span id="ubkp25" style="color: rgb(0,153,0)"><span id="ubkp26" style="color: black"><b id="bw6l9"><caption</b><span id="ubkp27" style="color: black"><b id="bw6l10">></b></span></span></span>音乐文件标题<span id="ubkp28" style="color: rgb(0,153,0)"><span id="ubkp29" style="color: black"><b id="bw6l11"></caption</b><span id="ubkp30" style="color: black"><b id="bw6l12">></b></span></span></span><br id="ubkp31" /> <span id="ubkp32" style="color: rgb(0,153,0)"><span id="ubkp33" style="color: black"><b id="bw6l13"><record</b><span id="ubkp34" style="color: black"><b id="bw6l14">></b></span></span></span>音乐录制信息<span id="ubkp35" style="color: rgb(0,153,0)"><span id="ubkp36" style="color: black"><b id="bw6l15"></record</b><span id="ubkp37" style="color: black"><b id="bw6l16">></b></span></span></span><br id="ubkp38" /> <span id="ubkp39" style="color: rgb(0,153,0)"><span id="ubkp40" style="color: black"><b id="bw6l17"></file</b><span id="ubkp41" style="color: black"><b id="bw6l18">></b></span></span></span><br id="ubkp42" /><span id="ubkp43" style="color: rgb(0,153,0)"><span id="ubkp44" style="color: black"><b id="bw6l19"></audio</b><span id="ubkp45" style="color: black"><b id="bw6l20">></b></span></span></span>
Of course, this XML file was provided in advance when I took the job. But I wanted to create such a file using DomIt! as the first part of this series, and also to show how powerful and easy-to-use DomIt! is.
The first thing we have to do is to call the DomIt! library file (go here, download, unzip the DomIt! library and put all the files into the same directory as the program file to be created below), and create a DomIt! document Example:
<span id="ubkp52" style="color: rgb(177,177,0)">require_once</span><span id="ubkp53" style="color: rgb(0,153,0)">(</span><span id="ubkp54" style="color: rgb(0,0,255)">xml_domit_include.php</span><span id="ubkp55" style="color: rgb(0,153,0)">)</span><span id="ubkp56" style="color: rgb(51,153,51)">;</span><br id="ubkp57" /><span id="ubkp58" style="color: rgb(0,0,51)">$xmlDoc</span><span id="ubkp59" style="color: rgb(51,153,51)">=&</span><span id="ubkp60" style="color: rgb(0,0,0)"><b id="bw6l21">new</b></span>DOMIT_Document<span id="ubkp61" style="color: rgb(0,153,0)">(</span><span id="ubkp62" style="color: rgb(0,153,0)">)</span><span id="ubkp63" style="color: rgb(51,153,51)">;</span><span id="ubkp126" style="color: rgb(102,102,102)"><i id="bw6l23">// 文档实例</i></span>
Note that the "&" symbol is for compatibility with PHP4.
This way we have an instance ready to use on demand. See the first line in the XML file content? That’s right, we’re going to add the XML file declaration:
<span id="ubkp68" style="color: rgb(0,0,51)">$xmlDecl</span><span id="ubkp69" style="color: rgb(51,153,51)">=&</span><span id="ubkp70" style="color: rgb(0,0,51)">$xmlDoc</span><span id="ubkp71" style="color: rgb(51,153,51)">-></span><span id="ubkp72" style="color: rgb(0,64,0)">createProcessingInstruction</span><span id="ubkp73" style="color: rgb(0,153,0)">(</span><span id="ubkp74" style="color: rgb(0,0,255)">xml</span><span id="ubkp75" style="color: rgb(51,153,51)">,</span><span id="ubkp76" style="color: rgb(0,0,255)">version="1.0"</span><span id="ubkp77" style="color: rgb(0,153,0)">)</span><span id="ubkp78" style="color: rgb(51,153,51)">;</span><br id="ubkp79" /><span id="ubkp80" style="color: rgb(0,0,51)">$xmlDoc</span><span id="ubkp81" style="color: rgb(51,153,51)">-></span><span id="ubkp82" style="color: rgb(0,64,0)">appendChild</span><span id="ubkp83" style="color: rgb(0,153,0)">(</span><span id="ubkp84" style="color: rgb(0,0,51)">$xmlDecl</span><span id="ubkp85" style="color: rgb(0,153,0)">)</span><span id="ubkp86" style="color: rgb(51,153,51)">;</span>
Here we can see that the declaration information is also treated as a child node, which is a reasonable definition. But here we can find that the createProcessingInstruction() method has an obvious shortcoming - there are only two declaration parameters (usually we may also define XML declaration information such as encoding). Fortunately we are using an open source library, which means we can easily modify it and customize it to suit our requirements. If you really need help modifying this particular method to add enough XML file declaration information (such as encoding, etc.), I'll cover that in the final article in this series.
Let’s get back to the point. After completing the XML file declaration part, what we see in the XML file content is the "audio" tag. It is the root element (root node) in this XML file. Let’s create this part:
<span id="ubkp92" style="color: rgb(0,0,51)">$rootElement</span><span id="ubkp93" style="color: rgb(51,153,51)">=&</span><span id="ubkp94" style="color: rgb(0,0,51)">$xmlDoc</span><span id="ubkp95" style="color: rgb(51,153,51)">-></span><span id="ubkp96" style="color: rgb(0,64,0)">createElement</span><span id="ubkp97" style="color: rgb(0,153,0)">(</span><span id="ubkp98" style="color: rgb(0,0,255)">audio</span><span id="ubkp99" style="color: rgb(0,153,0)">)</span><span id="ubkp100" style="color: rgb(51,153,51)">;</span><br id="ubkp101" /><span id="ubkp102" style="color: rgb(0,0,51)">$xmlDoc</span><span id="ubkp103" style="color: rgb(51,153,51)">-></span><span id="ubkp104" style="color: rgb(0,64,0)">appendChild</span><span id="ubkp105" style="color: rgb(0,153,0)">(</span><span id="ubkp106" style="color: rgb(0,0,51)">$rootElement</span><span id="ubkp107" style="color: rgb(0,153,0)">)</span><span id="ubkp108" style="color: rgb(51,153,51)">;</span>
You don’t need to worry about closing the tab, DomIt! has already done it for you. Using a similar method, we will create the "file" element, a child node of "audio"; within the "file" element, it also contains several sister elements "track", "caption" and "record" and their text content. Since a main method appendChild() is used when creating them, we can summarize it into one