search
HomeBackend DevelopmentPHP TutorialReading and writing XML and DOM with PHP_PHP tutorial

Reading and writing XML and DOM with PHP_PHP tutorial

Jul 13, 2016 pm 05:41 PM
domphpxmlScalableandhavemarkusewritelanguageread

Reading and writing Extensible Markup Language (XML) in PHP may seem a little scary. In fact, XML and all its related technologies can be scary, but reading and writing XML in PHP doesn't have to be a scary task. First, you need to learn a little about XML: what it is and what to do with it. Then, you need to learn how to read and write XML in PHP, and there are many ways to do this.

What is XML?

XML is a data storage format. It does not define what data is saved, nor does it define the format of the data. XML just defines tags and the attributes of those tags. Well-formed XML markup looks like this:

<ol class="dp-xml"><li class="alt"><span><span class="tag"><span class="tag-name">name</span><span class="tag">><font class="Apple-style-span" color="#000000"><span class="Apple-style-span" style="font-weight: normal;">This test for php100</span></font></span><span class="tag"></span><span class="tag-name">name</span><span class="tag">></span><span>  </span></span></span></li></ol>

The tag contains some text: Jack Herrington. An XML tag that contains no text looks like this:

<ol class="dp-xml"><li class="alt"><span><span class="tag"><span class="tag-name">powerUp</span><span> </span><span class="tag">/></span><span> </span></span></span></li></ol>

There is more than one way to write something in XML. For example, this tag forms the same output as the previous tag:

<ol class="dp-xml"><li class="alt"><span><span class="tag"><span class="tag-name">powerUp</span><span class="tag">></span><span class="tag"></span><span class="tag-name">powerUp</span><span class="tag">></span><span> </span></span></span></li></ol>

Attributes can also be added to XML tags. For example, this tag contains first and last attributes:

<ol class="dp-xml"><li class="alt"><span><span class="tag"><span class="tag-name">name</span><span> </span><span class="attribute">first</span><span>=</span><span class="attribute-value">"Jack"</span><span> </span><span class="attribute">last</span><span>=</span><span class="attribute-value">"Herrington"</span><span> </span><span class="tag">/></span><span> </span></span></span></li></ol>

Special characters can also be encoded in XML. For example, the & symbol can be encoded like this:

<ol class="dp-xml"><li class="alt"><span><span>& </span></span></li></ol>

An XML file containing tags and attributes is well-formed if it is formatted like the example, meaning that the tags are symmetrical and the characters are encoded correctly. Listing 1 is an example of well-formed XML.

List 1. XML book list example

<ol class="dp-xml">
<li class="alt"><span><span class="tag"><span class="tag-name">books</span><span class="tag">></span><span>  </span></span></span></li>
<li><span><span class="tag"><span class="tag-name">book</span><span class="tag">></span><span>  </span></span></span></li>
<li class="alt"><span><span class="tag"><span class="tag-name">author</span><span class="tag">></span><span>Jack Herrington</span><span class="tag"></span><span class="tag-name">author</span><span class="tag">></span><span>  </span></span></span></li>
<li><span><span class="tag"><span class="tag-name">title</span><span class="tag">></span><span>PHP Hacks</span><span class="tag"></span><span class="tag-name">title</span><span class="tag">></span><span>  </span></span></span></li>
<li class="alt"><span><span class="tag"><span class="tag-name">publisher</span><span class="tag">></span><span>OReilly</span><span class="tag"></span><span class="tag-name">publisher</span><span class="tag">></span><span>  </span></span></span></li>
<li><span><span class="tag"></span><span class="tag-name">book</span><span class="tag">></span><span>  </span></span></li>
<li class="alt"><span><span class="tag"><span class="tag-name">book</span><span class="tag">></span><span>  </span></span></span></li>
<li><span><span class="tag"><span class="tag-name">author</span><span class="tag">></span><span>Jack Herrington</span><span class="tag"></span><span class="tag-name">author</span><span class="tag">></span><span>  </span></span></span></li>
<li class="alt"><span><span class="tag"><span class="tag-name">title</span><span class="tag">></span><span>Podcasting Hacks</span><span class="tag"></span><span class="tag-name">title</span><span class="tag">></span><span>  </span></span></span></li>
<li><span><span class="tag"><span class="tag-name">publisher</span><span class="tag">></span><span>OReilly</span><span class="tag"></span><span class="tag-name">publisher</span><span class="tag">></span><span>  </span></span></span></li>
<li class="alt"><span><span class="tag"></span><span class="tag-name">book</span><span class="tag">></span><span>  </span></span></li>
<li><span><span class="tag"></span><span class="tag-name">books</span><span class="tag">></span><span>  </span></span></li>
</ol>

The XML in Listing 1 contains a list of books. The parent tag contains a set of tags, each of which contains , , and <publisher> tags. An XML document is correct when its markup structure and content are verified by an external schema file. Schema files can be specified in different formats. For the purposes of this article, all that is needed is well-formed XML. </publisher>

If you think XML looks a lot like Hypertext Markup Language (HTML), you're right. XML and HTML are both markup-based languages ​​and they have many similarities. However, it is important to note that while an XML document may be well-formed HTML, not all HTML documents are well-formed XML. The newline tag (br) is a good example of the difference between XML and HTML. This newline tag is well-formed HTML, but not well-formed XML:

<ol class="dp-xml">
<li class="alt"><span><span class="tag"><span class="tag-name">p</span><span class="tag">></span><span>This is a paragraph</span><span class="tag"><span class="tag-name">br</span><span class="tag">></span><span> </span></span></span></span></li>
<li>
<span>With a line break</span><span class="tag"></span><span class="tag-name">p</span><span class="tag">></span><span>  </span>
</li>
</ol>

This newline tag is well-formed XML and HTML:

<ol class="dp-xml">
<li class="alt"><span><span class="tag"><span class="tag-name">p</span><span class="tag">></span><span>This is a paragraph</span><span class="tag"><span class="tag-name">br</span><span> </span><span class="tag">/></span><span> </span></span></span></span></li>
<li>
<span>With a line break</span><span class="tag"></span><span class="tag-name">p</span><span class="tag">></span><span>  </span>
</li>
</ol>

To write HTML as well-formed XML, follow the W3C committee's Extensible Hypertext Markup Language (XHTML) standard (see Resources). All modern browsers can render XHTML. Furthermore, you can use XML tools to read XHTML and find the data in the document, which is much easier than parsing HTML.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/486120.htmlTechArticleReading and writing Extensible Markup Language (XML) with PHP may seem a little scary. In fact, XML and all its related technologies can be scary, but reading and writing XML in PHP doesn't have to be...
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
PHP Performance Tuning for High Traffic WebsitesPHP Performance Tuning for High Traffic WebsitesMay 14, 2025 am 12:13 AM

ThesecrettokeepingaPHP-poweredwebsiterunningsmoothlyunderheavyloadinvolvesseveralkeystrategies:1)ImplementopcodecachingwithOPcachetoreducescriptexecutiontime,2)UsedatabasequerycachingwithRedistolessendatabaseload,3)LeverageCDNslikeCloudflareforservin

Dependency Injection in PHP: Code Examples for BeginnersDependency Injection in PHP: Code Examples for BeginnersMay 14, 2025 am 12:08 AM

You should care about DependencyInjection(DI) because it makes your code clearer and easier to maintain. 1) DI makes it more modular by decoupling classes, 2) improves the convenience of testing and code flexibility, 3) Use DI containers to manage complex dependencies, but pay attention to performance impact and circular dependencies, 4) The best practice is to rely on abstract interfaces to achieve loose coupling.

PHP Performance: is it possible to optimize the application?PHP Performance: is it possible to optimize the application?May 14, 2025 am 12:04 AM

Yes,optimizingaPHPapplicationispossibleandessential.1)ImplementcachingusingAPCutoreducedatabaseload.2)Optimizedatabaseswithindexing,efficientqueries,andconnectionpooling.3)Enhancecodewithbuilt-infunctions,avoidingglobalvariables,andusingopcodecaching

PHP Performance Optimization: The Ultimate GuidePHP Performance Optimization: The Ultimate GuideMay 14, 2025 am 12:02 AM

ThekeystrategiestosignificantlyboostPHPapplicationperformanceare:1)UseopcodecachinglikeOPcachetoreduceexecutiontime,2)Optimizedatabaseinteractionswithpreparedstatementsandproperindexing,3)ConfigurewebserverslikeNginxwithPHP-FPMforbetterperformance,4)

PHP Dependency Injection Container: A Quick StartPHP Dependency Injection Container: A Quick StartMay 13, 2025 am 12:11 AM

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Dependency Injection vs. Service Locator in PHPDependency Injection vs. Service Locator in PHPMay 13, 2025 am 12:10 AM

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHP performance optimization strategies.PHP performance optimization strategies.May 13, 2025 am 12:06 AM

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHP Email Validation: Ensuring Emails Are Sent CorrectlyPHP Email Validation: Ensuring Emails Are Sent CorrectlyMay 13, 2025 am 12:06 AM

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools