浅谈如何利用PHP访问和操作DOM(1)_PHP教程

php中文网
发布: 2016-07-15 13:26:14
原创
1207人浏览过

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代码压缩包,

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/446612.htmlTechArticleDOM树定义了文档的逻辑结构,以及控制你访问和操作这些文档的方法。使用DOM,开发人员可以创建XML或HTML文档,操作它们的结果,增加、修...
PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号