search
HomeBackend DevelopmentPHP TutorialUse DomIt! to do simple XML processing_PHP tutorial

Use DomIt! to do simple XML processing_PHP tutorial

Jul 13, 2016 pm 05:38 PM
xmlindivualDodeal withWorkdocumentyeshaveuseofSimplethis

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

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/486494.htmlTechArticleOnce 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 to do is...
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

PHP and Python are both high-level programming languages ​​that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.