浅谈如何利用PHP访问和操作DOM(1)_PHP教程
DOM树定义了文档的逻辑结构,以及控制你访问和操作这些文档的方法。使用DOM,开发人员可以创建XML或HTML文档,操作它们的结果,增加、修改和删除文档 元素及内容。可以从任何编程语言访问DOM,本文使用PHP 5 DOM扩展,它是PHP核心的一部分,因此除了PHP外,不需要安装其它软件。
DOM树节点遵循XML命名规范,如:
1、Document节点 -- 表示DOMDocument接口
2、Element节点 -- 表示DOMElement接口
3、Attribute节点 -- 表示DOMAttr接口
4、Comment节点 -- 表示DOMComment接口
5、Text节点 -- 表示DOMText接口
提取元素
这一小节介绍如何从DOM树中提取元素和值,本文使用Book.xml作为例子进行说明,其内容如清单1所示。
清单1 Book.xml
<ol class="dp-xml"> <li class="alt"><span><strong><font color="#006699"><span class="tag"></span><span class="tag-" name>xml</span></font></strong><span> </span><span class="attribute"><font color="#ff0000">version</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">"1.0"</font></span><span> </span><span class="attribute"><font color="#ff0000">encoding</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">"UTF-8"</font></span><span> </span><span class="attribute"><font color="#ff0000">standalone</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">"yes"</font></span><span> </span><span class="tag"><strong><font color="#006699">?></font></strong></span><span> </span></span></li> <li> <span></span><strong><font color="#006699"><span class="tag"><span class="tag-" name>book</span><span class="tag">></span></span></font></strong><span> </span> </li> <li class="alt"> <span></span><span class="comments"><font color="#008200"><!--XML Processing [part I] --></font></span><span> </span> </li> <li> <span> </span><strong><font color="#006699"><span class="tag"><span class="tag-" name>name</span><span class="tag">></span></span></font></strong><span>XML Processing I</span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">name</span><span class="tag">></span></font></strong><span> </span> </li> <li class="alt"> <span> </span><strong><font color="#006699"><span class="tag"><span class="tag-name">author</span><span class="tag">></span></span></font></strong><span>John Smith Jr.</span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">author</span><span class="tag">></span></font></strong><span> </span> </li> <li> <span> </span><strong><font color="#006699"><span class="tag"><span class="tag-name">publisher</span><span class="tag">></span></span></font></strong><span>HisOwnTM</span><strong><font color="#006699"><span class="tag"></span><span class="tag-" name>publisher</span><span class="tag">></span></font></strong><span> </span> </li> <li class="alt"> <span> </span><strong><font color="#006699"><span class="tag"><span class="tag-name">ISBN</span><span class="tag">></span></span></font></strong><span>111-222-333-4441</span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">ISBN</span><span class="tag">></span></font></strong><span> </span> </li> <li> <span> </span><strong><font color="#006699"><span class="tag"><span class="tag-name">contents</span><span class="tag">></span></span></font></strong><span> </span> </li> <li class="alt"> <span> </span><strong><font color="#006699"><span class="tag"><span class="tag-" name>chapter_I</span><span class="tag">></span></span></font></strong><span> </span> </li> <li> <span> </span><strong><font color="#006699"><span class="tag"><span class="tag-name">title</span><span class="tag">></span></span></font></strong><span>What is XML about ?</span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">title</span><span class="tag">></span></font></strong><span> </span> </li> <li class="alt"> <span> </span><strong><font color="#006699"><span class="tag"><span class="tag-" name>content</span><span class="tag">></span></span></font></strong><span>XML (Extensible Markup Language) is a ...</span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">content</span><span class="tag">></span></font></strong><span> </span> </li> <li> <span> </span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">chapter_I</span><span class="tag">></span></font></strong><span> </span> </li> <li class="alt"> <span> </span><strong><font color="#006699"><span class="tag"><span class="tag-" name>chapter_II</span><span class="tag">></span></span></font></strong><span> </span> </li> <li> <span> </span><strong><font color="#006699"><span class="tag"><span class="tag-name">title</span><span class="tag">></span></span></font></strong><span>SAX</span><strong><font color="#006699"><span class="tag"></span><span class="tag-" name>title</span><span class="tag">></span></font></strong><span> </span> </li> <li class="alt"> <span> </span><strong><font color="#006699"><span class="tag"><span class="tag-" name>content</span><span class="tag">></span></span></font></strong><span>SAX is a simple API for ...</span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">content</span><span class="tag">></span></font></strong><span> </span> </li> <li> <span> </span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">chapter_II</span><span class="tag">></span></font></strong><span> </span> </li> <li class="alt"> <span> </span><strong><font color="#006699"><span class="tag"><span class="tag-" name>chapter_III</span><span class="tag">></span></span></font></strong><span> </span> </li> <li> <span> </span><strong><font color="#006699"><span class="tag"><span class="tag-name">title</span><span class="tag">></span></span></font></strong><span>StAX</span><strong><font color="#006699"><span class="tag"></span><span class="tag-" name>title</span><span class="tag">></span></font></strong><span> </span> </li> <li class="alt"> <span> </span><strong><font color="#006699"><span class="tag"><span class="tag-" name>content</span><span class="tag">></span></span></font></strong><span>Much powerful and flexible, StAX, is very...</span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">content</span><span class="tag">></span></font></strong><span> </span> </li> <li> <span> </span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">chapter_III</span><span class="tag">></span></font></strong><span> </span> </li> <li class="alt"> <span> </span><strong><font color="#006699"><span class="tag"><span class="tag-" name>chapter_IV</span><span class="tag">></span></span></font></strong><span> </span> </li> <li> <span> </span><strong><font color="#006699"><span class="tag"><span class="tag-name">title</span><span class="tag">></span></span></font></strong><span>DOM </span> </li> <li class="alt"> <span> </span><strong><font color="#006699"><span class="tag"><span class="tag-name">subtitle</span><span class="tag">></span></span></font></strong><span>DOM concept </span> </li> <li> <span> </span><strong><font color="#006699"><span class="tag"><span class="tag-name">continut</span><span class="tag">></span></span></font></strong><span>Starting to use DOM...</span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">continut</span><span class="tag">></span></font></strong><span> </span> </li> <li class="alt"> <span> </span><strong><font color="#006699"><span class="tag"></span><span class="tag">subtitle</span><span class="tag">></span></font></strong><span> </span> </li> <li> <span> </span><strong><font color="#006699"><span class="tag"><span class="tag-name">subchapter_IV_I</span><span class="tag">></span></span></font></strong><span> </span> </li> <li class="alt"> <span> </span><strong><font color="#006699"><span class="tag"><span class="tag-name">title</span><span class="tag">></span></span></font></strong><span>First DOM application...</span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">title</span><span class="tag">></span></font></strong><span> </span> </li> <li> <span> </span><st rong><font color="#006699"><span class="tag"><span class="tag-name">content</span><span class="tag">></span></span></font><span>Here it is your first DOM application...</span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">content</span><span class="tag">></span></font></strong><span> </span><li class="alt"> <span> </span><strong><font color="#006699"><span class="tag"></span><span class="tag">subchapter_IV_I</span><span class="tag">></span></font></strong><span> </span> </li> <li> <span> </span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">title</span><span class="tag">></span></font></strong><span> </span> </li> <li class="alt"> <span> </span><strong><font color="#006699"><span class="tag"></span><span class="tag-" name>chapter_IV</span><span class="tag">></span></font></strong><span> </span> </li> <li> <span> </span><strong><font color="#006699"><span class="tag"><span class="tag-" name>end</span><span class="tag">></span></span></font></strong><span>The end...</span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">end</span><span class="tag">></span></font></strong><span> </span> </li> <li class="alt"> <span> </span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">contents</span><span class="tag">></span></font></strong><span> </span> </li> <li> <span></span><span class="comments"><font color="#008200"><!-- See you in XML Processing [part II] --></font></span><span> </span> </li> <li class="alt"> <span></span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">book</span><span class="tag">></span></font></strong><span> </span> </li></st> </li> </ol>
先下载本文使用的PHP代码压缩包,

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

PHP 8.4 带来了多项新功能、安全性改进和性能改进,同时弃用和删除了大量功能。 本指南介绍了如何在 Ubuntu、Debian 或其衍生版本上安装 PHP 8.4 或升级到 PHP 8.4

CakePHP 是 PHP 的开源框架。它的目的是使应用程序的开发、部署和维护变得更加容易。 CakePHP 基于类似 MVC 的架构,功能强大且易于掌握。模型、视图和控制器 gu

Visual Studio Code,也称为 VS Code,是一个免费的源代码编辑器 - 或集成开发环境 (IDE) - 可用于所有主要操作系统。 VS Code 拥有针对多种编程语言的大量扩展,可以轻松编写
