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

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



1. First, we right-click the blank space of the taskbar and select the [Task Manager] option, or right-click the start logo, and then select the [Task Manager] option. 2. In the opened Task Manager interface, we click the [Services] tab on the far right. 3. In the opened [Service] tab, click the [Open Service] option below. 4. In the [Services] window that opens, right-click the [InternetConnectionSharing(ICS)] service, and then select the [Properties] option. 5. In the properties window that opens, change [Open with] to [Disabled], click [Apply] and then click [OK]. 6. Click the start logo, then click the shutdown button, select [Restart], and complete the computer restart.

When deleting or decompressing a folder on your computer, sometimes a prompt dialog box "Error 0x80004005: Unspecified Error" will pop up. How should you solve this situation? There are actually many reasons why the error code 0x80004005 is prompted, but most of them are caused by viruses. We can re-register the dll to solve the problem. Below, the editor will explain to you the experience of handling the 0x80004005 error code. Some users are prompted with error code 0X80004005 when using their computers. The 0x80004005 error is mainly caused by the computer not correctly registering certain dynamic link library files, or by a firewall that does not allow HTTPS connections between the computer and the Internet. So how about

Quark Netdisk and Baidu Netdisk are currently the most commonly used Netdisk software for storing files. If you want to save the files in Quark Netdisk to Baidu Netdisk, how do you do it? In this issue, the editor has compiled the tutorial steps for transferring files from Quark Network Disk computer to Baidu Network Disk. Let’s take a look at how to operate it. How to save Quark network disk files to Baidu network disk? To transfer files from Quark Network Disk to Baidu Network Disk, you first need to download the required files from Quark Network Disk, then select the target folder in the Baidu Network Disk client and open it. Then, drag and drop the files downloaded from Quark Cloud Disk into the folder opened by the Baidu Cloud Disk client, or use the upload function to add the files to Baidu Cloud Disk. Make sure to check whether the file was successfully transferred in Baidu Cloud Disk after the upload is completed. That's it

Recently, many netizens have asked the editor, what is the file hiberfil.sys? Can hiberfil.sys take up a lot of C drive space and be deleted? The editor can tell you that the hiberfil.sys file can be deleted. Let’s take a look at the details below. hiberfil.sys is a hidden file in the Windows system and also a system hibernation file. It is usually stored in the root directory of the C drive, and its size is equivalent to the size of the system's installed memory. This file is used when the computer is hibernated and contains the memory data of the current system so that it can be quickly restored to the previous state during recovery. Since its size is equal to the memory capacity, it may take up a larger amount of hard drive space. hiber

In the process of PHP development, dealing with special characters is a common problem, especially in string processing, special characters are often escaped. Among them, converting special characters into single quotes is a relatively common requirement, because in PHP, single quotes are a common way to wrap strings. In this article, we will explain how to handle special character conversion single quotes in PHP and provide specific code examples. In PHP, special characters include but are not limited to single quotes ('), double quotes ("), backslash (), etc. In strings

Detailed explanation of the role of .ibd files in MySQL and related precautions MySQL is a popular relational database management system, and the data in the database is stored in different files. Among them, the .ibd file is a data file in the InnoDB storage engine, used to store data and indexes in tables. This article will provide a detailed analysis of the role of the .ibd file in MySQL and provide relevant code examples to help readers better understand. 1. The role of .ibd files: storing data: .ibd files are InnoDB storage

In Linux systems, you can use the following command to view the contents of the log file: tail command: The tail command is used to display the content at the end of the log file. It is a common command to view the latest log information. tail [option] [file name] Commonly used options include: -n: Specify the number of lines to be displayed, the default is 10 lines. -f: Monitor the file content in real time and automatically display the new content when the file is updated. Example: tail-n20logfile.txt#Display the last 20 lines of the logfile.txt file tail-flogfile.txt#Monitor the updated content of the logfile.txt file in real time head command: The head command is used to display the beginning of the log file

Working with files in the Linux operating system requires the use of various commands and techniques that enable developers to efficiently create and execute files, code, programs, scripts, and other things. In the Linux environment, files with the extension ".a" have great importance as static libraries. These libraries play an important role in software development, allowing developers to efficiently manage and share common functionality across multiple programs. For effective software development in a Linux environment, it is crucial to understand how to create and run ".a" files. This article will introduce how to comprehensively install and configure the Linux ".a" file. Let's explore the definition, purpose, structure, and methods of creating and executing the Linux ".a" file. What is L
