simplexml提供了一种处理XML的方便方法,但是对于复杂的任务,PHP的DOM(文档对象模型)提供了出色的控制。 W3C标准实现DOM提供了一种更强大的面向对象的方法,与单纯板相比。虽然最初是复杂的,但掌握DOM具有广泛的操纵功能。 本文通过ALibrary
>类添加,删除和查询XML库目录中的书籍的基本DOM功能。
密钥概念:
Library
元素和属性处理:createElement()
setAttribute()
class:
>此类提供了上面概述的功能的方法。 错误处理和面向对象的最佳实践被简化为清晰。
<!DOCTYPE library [ <!ELEMENT library (book*)> <!ELEMENT book (title, author, genre, chapter*)> <!ATTLIST book isbn ID #REQUIRED> <!ELEMENT title (#PCDATA)> <!ELEMENT author (#PCDATA)> <!ELEMENT genre (#PCDATA)> <!ELEMENT chapter (chaptitle, text)> <!ATTLIST chapter position NMTOKEN #REQUIRED> <!ELEMENT chaptitle (#PCDATA)> <!ELEMENT text (#PCDATA)> ]>
<?xml version="1.0" encoding="utf-8"?> <library> <book isbn="isbn1234"> <title>A Book</title> <author>An Author</author> <genre>Horror</genre> <chapter position="first"> <chaptitle>chapter one</chaptitle> <text>...</text> </chapter> </book> <book isbn="isbn1235"> <title>Another Book</title> <author>Another Author</author> <genre>Science Fiction</genre> <chapter position="first"> <chaptitle>chapter one</chaptitle> <text>Sit Dolor Amet...</text> </chapter> </book> </library>
(注意:Library
,
。
以上是PHP DOM:使用XML的详细内容。更多信息请关注PHP中文网其他相关文章!