Heim > Artikel > Backend-Entwicklung > 用PHP读取和编写XML及DOM_PHP教程
用PHP读取和编写可扩展标记语言(XML)看起来可能有点恐怖。实际上,XML 和它的所有相关技术可能是恐怖的,但是用PHP读取和编写XML不一定是项恐怖的任务。首先,需要学习一点关于XML的知识:它是什么,用它做什么。然后,需要学习如何用PHP读取和编写XML,而有许多种方式可以做这件事。
什么是XML?
XML是一种数据存储格式。它没有定义保存什么数据,也没有定义数据的格式。XML只是定义了标记和这些标记的属性。格式良好的XML标记看起来像这样:
<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>
这个
<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>
用 XML 对某件事进行编写的方式不止一种。例如,这个标记形成的输出与前一个标记相同:
<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>
也可以向 XML 标记添加属性。例如,这个
<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>
也可以用 XML 对特殊字符进行编码。例如,& 符号可以像这样编码:
<ol class="dp-xml"><li class="alt"><span><span>& </span></span></li></ol>
包含标记和属性的 XML 文件如果像示例一样格式化,就是格式良好的,这意味着标记是对称的,字符的编码正确。清单 1 是一份格式良好的 XML 的示例。
清单 1. XML图书列表示例
<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>
清单 1 中的 XML 包含一个图书列表。父标记
如果觉得 XML 看起来很像超文本标记语言(HTML),那么就对了。XML 和 HTML 都是基于标记的语言,它们有许多相似之处。但是,要着重指出的是:虽然 XML 文档可能是格式良好的 HTML,但不是所有的 HTML 文档都是格式良好的 XML。换行标记(br)是 XML 和 HTML 之间区别的一个好例子。这个换行标记是格式良好的 HTML,但不是格式良好的 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>
这个换行标记是格式良好的 XML 和 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>
如果要把 HTML 编写成同样是格式良好的 XML,请遵循 W3C 委员会的可扩展超文本标记语言(XHTML)标准(参见 参考资料)。所有现代的浏览器都能呈现 XHTML。而且,还可以用 XML 工具读取 XHTML 并找出文档中的数据,这比解析 HTML 容易得多。