Heim >Backend-Entwicklung >PHP-Tutorial >浅谈如何利用PHP访问和操作DOM(1)_PHP教程

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

WBOY
WBOYOriginal
2016-07-15 13:26:141078Durchsuche

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

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/446612.htmlTechArticleDOM树定义了文档的逻辑结构,以及控制你访问和操作这些文档的方法。使用DOM,开发人员可以创建XML或HTML文档,操作它们的结果,增加、修...
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn