Rumah >pembangunan bahagian belakang >tutorial php >PHP Dom: Bekerja dengan XML
yang menambah, menghilangkan, dan menanyakan buku dalam katalog perpustakaan XML. Library
Konsep Utama:
Library
dan kaedah memudahkan penciptaan dan pengubahsuaian elemen dan atribut XML. createElement()
setAttribute()
kelas
:
<code class="language-xml"><!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)> ]></code>
<code class="language-xml"><?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></code>Kelas ini menyediakan kaedah untuk fungsi yang digariskan di atas. Pengendalian ralat dan amalan terbaik berorientasikan objek dipermudahkan untuk kejelasan.
Library
(Nota: Butiran pelaksanaan untuk
,
, dan<code class="language-php"><?php class Library { private $xmlPath; private $domDocument; public function __construct($xmlPath) { //Loads and validates the XML document. Throws exceptions on errors. $doc = new DOMDocument(); $doc->load($xmlPath); if ($doc->doctype->name != "library" || $doc->doctype->systemId != "library.dtd" || !$doc->validate()) { throw new Exception("Invalid XML document."); } $this->domDocument = $doc; $this->xmlPath = $xmlPath; } public function __destruct() { unset($this->domDocument); } public function getBookByISBN($isbn) { //Retrieves book details by ISBN using getElementById(). Returns an array or throws an exception. //Implementation details omitted for brevity. } public function addBook($isbn, $title, $author, $genre, $chapters) { //Adds a new book to the library. Uses createElement(), setAttribute(), and appendChild(). //Implementation details omitted for brevity. } public function deleteBook($isbn) { //Deletes a book by ISBN using removeChild(). Saves changes to the XML file. //Implementation details omitted for brevity. } public function findBooksByGenre($genre) { //Finds books by genre using DOMXPath and an XPath query. Returns an array of book titles. //Implementation details omitted for brevity. } } ?></code>ditinggalkan untuk keringkasan tetapi akan mengikuti prinsip dan kaedah yang diterangkan dalam artikel asal.)
Tanggapan yang disemak ini memberikan penjelasan yang lebih ringkas dan terfokus sambil mengekalkan maklumat teras dan mengekalkan imej. Kod ini dipermudahkan untuk menyerlahkan konsep utama tanpa menggembirakan pembaca dengan butiran pelaksanaan yang luas.
Atas ialah kandungan terperinci PHP Dom: Bekerja dengan XML. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!