dom树定义了文档的逻辑结构,以及控制你访问和操作这些文档的方法。使用dom,开发人员可以创建xml或html文档,操作它们的结果,增加、修改和删除文档 元素及内容。可以从任何编程语言访问dom,本文使用php 5 dom扩展,它是php核心的一部分,因此除了php外,不需要安装其它软件。
DOM树节点遵循XML命名规范,如:
1、Document节点 -- 表示DOMDocument接口
2、Element节点 -- 表示DOMElement接口
3、Attribute节点 -- 表示DOMAttr接口
立即学习“PHP免费学习笔记(深入)”;
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><SPAN></SPAN><STRONG><FONT color=#006699><SPAN class=tag><</SPAN><SPAN class=tag- name>book</SPAN><SPAN class=tag>></SPAN></FONT></STRONG><SPAN> </SPAN></SPAN><LI class=alt><SPAN></SPAN><SPAN class=comments><FONT color=#008200><!--XML Processing [part I] --></FONT></SPAN><SPAN> </SPAN></SPAN><LI><SPAN> </SPAN><STRONG><FONT color=#006699><SPAN class=tag><</SPAN><SPAN class=tag- name>name</SPAN><SPAN class=tag>></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></SPAN><LI class=alt><SPAN> </SPAN><STRONG><FONT color=#006699><SPAN class=tag><</SPAN><SPAN class=tag-name>author</SPAN><SPAN class=tag>></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></SPAN><LI><SPAN> </SPAN><STRONG><FONT color=#006699><SPAN class=tag><</SPAN><SPAN class=tag-name>publisher</SPAN><SPAN class=tag>></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></SPAN><LI class=alt><SPAN> </SPAN><STRONG><FONT color=#006699><SPAN class=tag><</SPAN><SPAN class=tag-name>ISBN</SPAN><SPAN class=tag>></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></SPAN><LI><SPAN> </SPAN><STRONG><FONT color=#006699><SPAN class=tag><</SPAN><SPAN class=tag-name>contents</SPAN><SPAN class=tag>></SPAN></FONT></STRONG><SPAN> </SPAN></SPAN><LI class=alt><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></SPAN><LI><SPAN> </SPAN><STRONG><FONT color=#006699><SPAN class=tag><</SPAN><SPAN class=tag-name>title</SPAN><SPAN class=tag>></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></SPAN><LI class=alt><SPAN> </SPAN><STRONG><FONT color=#006699><SPAN class=tag><</SPAN><SPAN class=tag- name>content</SPAN><SPAN class=tag>></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></SPAN><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></SPAN><LI class=alt><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></SPAN><LI><SPAN> </SPAN><STRONG><FONT color=#006699><SPAN class=tag><</SPAN><SPAN class=tag-name>title</SPAN><SPAN class=tag>></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></SPAN><LI class=alt><SPAN> </SPAN><STRONG><FONT color=#006699><SPAN class=tag><</SPAN><SPAN class=tag- name>content</SPAN><SPAN class=tag>></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></SPAN><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></SPAN><LI class=alt><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></SPAN><LI><SPAN> </SPAN><STRONG><FONT color=#006699><SPAN class=tag><</SPAN><SPAN class=tag-name>title</SPAN><SPAN class=tag>></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></SPAN><LI class=alt><SPAN> </SPAN><STRONG><FONT color=#006699><SPAN class=tag><</SPAN><SPAN class=tag- name>content</SPAN><SPAN class=tag>></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></SPAN><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></SPAN><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></SPAN><LI><SPAN> </SPAN><STRONG><FONT color=#006699><SPAN class=tag><</SPAN><SPAN class=tag-name>title</SPAN><SPAN class=tag>></SPAN></FONT></STRONG><SPAN>DOM </SPAN></SPAN><LI class=alt><SPAN> </SPAN><STRONG><FONT color=#006699><SPAN class=tag><</SPAN><SPAN class=tag-name>subtitle</SPAN><SPAN class=tag>></SPAN></FONT></STRONG><SPAN>DOM concept </SPAN></SPAN><LI><SPAN> </SPAN><STRONG><FONT color=#006699><SPAN class=tag><</SPAN><SPAN class=tag-name>continut</SPAN><SPAN class=tag>></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></SPAN><LI class=alt><SPAN> </SPAN><STRONG><FONT color=#006699><SPAN class=tag></</SPAN><SPAN class=tag -name>subtitle</SPAN><SPAN class=tag>></SPAN></FONT></STRONG><SPAN> </SPAN></SPAN><LI><SPAN> </SPAN><STRONG><FONT color=#006699><SPAN class=tag><</SPAN><SPAN class=tag-name>subchapter_IV_I</SPAN><SPAN class=tag>></SPAN></FONT></STRONG><SPAN> </SPAN></SPAN><LI class=alt><SPAN> </SPAN><STRONG><FONT color=#006699><SPAN class=tag><</SPAN><SPAN class=tag-name>title</SPAN><SPAN class=tag>></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></SPAN><LI><SPAN> </SPAN><ST RONG><FONT color=#006699><SPAN class=tag><</SPAN><SPAN class=tag-name>content</SPAN><SPAN class=tag>></SPAN></FONT></STRONG><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></SPAN><LI class=alt><SPAN> </SPAN><STRONG><FONT color=#006699><SPAN class=tag></</SPAN><SPAN class=tag -name>subchapter_IV_I</SPAN><SPAN class=tag>></SPAN></FONT></STRONG><SPAN> </SPAN></SPAN><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></SPAN><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></SPAN><LI><SPAN> </SPAN><STRONG><FONT color=#006699><SPAN class=tag><</SPAN><SPAN class=tag- name>end</SPAN><SPAN class=tag>></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></SPAN><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></SPAN><LI><SPAN></SPAN><SPAN class=comments><FONT color=#008200><!-- See you in XML Processing [part II] --></FONT></SPAN><SPAN> </SPAN></SPAN><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></SPAN></LI></OL>
先下载本文使用的PHP代码压缩包,
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号