首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 網頁程式設計 >> PHP 程式設計 >> PHP 綜合知識

PHP XML 應用開發基礎:DOM、SimpleXML、XPath 完整指南

     編輯:PHP 綜合知識

XML 簡介

XML(eXtensible Markup Language)是 W3C 標準,設計目標是「傳輸與存儲數據」,而非顯示數據。其標簽自定義、結構嚴謹、跨平台特性使其成為異構系統數據交換的通用格式。常見應用場景:XHTML、XML-RPC、RSS、AJAX、配置文件、Web Services(SOAP/REST)。

示例數據源:圖書館目錄

<?xml version="1.0"?>
<library>
  <categories>
    <category cid="1">Web Development</category>
    <category cid="2">Database Programming</category>
    <category cid="3">PHP</category>
    <category cid="4">Java</category>
  </categories>
  <books>
    <book>
      <title>Apache 2</title>
      <author>Peter Wainwright</author>
      <publisher>Wrox</publisher>
      <category>1</category>
    </book>
    <book>
      <title>Advanced PHP Programming</title>
      <author>George Schlossnagle</author>
      <publisher>Developer Library</publisher>
      <category>1</category>
      <category>3</category>
    </book>
    <book>
      <title>Visual FoxPro 6 - Programmers Guide</title>
      <author>Eric Stroo</author>
      <publisher>Microsoft Press</publisher>
      <category>2</category>
    </book>
    <book>
      <title>Mastering Java 2</title>
      <author>John Zukowski</author>
      <publisher>Sybex</publisher>
      <category>4</category>
    </book>
  </books>
</library>

方案一:DOMDocument(W3C 標準、功能最全)

適用於需頻繁修改結構(增刪節點、屬性)、大文檔流式處理、跨語言移植(JS/Java/Python DOM API 一致)。

讀取與遍歷

<?php
$dom = new DOMDocument('1.0');
$dom->load('xml/library.xml');

// 建立分類 ID -> 名稱映射
$categories = [];
$catNodes = $dom->getElementsByTagName('categories')->item(0)
                ->getElementsByTagName('category');
foreach ($catNodes as $node) {
    $cid = $node->getAttribute('cid');
    $categories[$cid] = $node->nodeValue;  // nodeValue 等同 firstChild->nodeValue
}

// 輸出圖書信息
foreach ($dom->getElementsByTagName('book') as $book) {
    $title  = $book->getElementsByTagName('title')->item(0)->nodeValue;
    $author = $book->getElementsByTagName('author')->item(0)->nodeValue;
    
    $catList = [];
    foreach ($book->getElementsByTagName('category') as $cat) {
        $catList[] = $categories[$cat->nodeValue];
    }
    
    echo "<div><h2>$title</h2>";
    echo "<p><b>Author:</b> $author</p>";
    echo "<p><b>Categories:</b> " . implode('、', $catList) . "</p></div>";
}
?>

新增節點

function addCategory(DOMDocument $dom, $cid, $name) {
    $catNode = $dom->createElement('category');
    $catNode->setAttribute('cid', $cid);
    $catNode->appendChild($dom->createTextNode($name));
    
    $categories = $dom->getElementsByTagName('categories')->item(0);
    $categories->appendChild($catNode);
}

// 使用
addCategory($dom, 5, 'Go Language');
$dom->save('xml/library.xml');  // 持久化

保存輸出

  • $dom->save('file.xml'):直接寫入文件
  • $dom->saveXML($node):返回節點或整棵樹的 XML 字符串

方案二:SimpleXML(語法最簡潔、適合純讀取)

將 XML 映射為對象/數組混合結構,屬性用 ['attr'] 訪問,元素用 ->element 或 ->element[0] 訪問,文本需 (string) 強制轉換。

讀取示例

<?php
$xml = simplexml_load_file('xml/library.xml');

// 分類映射
$categories = [];
foreach ($xml->categories->category as $cat) {
    $categories[(string)$cat['cid']] = (string)$cat;
}

// 輸出圖書
foreach ($xml->books->book as $book) {
    $catList = [];
    foreach ($book->category as $cat) {
        $catList[] = $categories[(string)$cat];
    }
    echo "<div><h2>{$book->title}</h2>";
    echo "<p><b>Author:</b> {$book->author}</p>";
    echo "<p><b>Categories:</b> " . implode('、', $catList) . "</p></div>";
}
?>

修改 XML(需借助 DOM 互轉)

function addCategory(SimpleXMLElement &$sxml, $cid, $name) {
    $dom = new DOMDocument();
    $dom->loadXML($sxml->asXML());  // SimpleXML -> DOM
    
    $cat = $dom->createElement('category');
    $cat->setAttribute('cid', $cid);
    $cat->appendChild($dom->createTextNode($name));
    
    $dom->getElementsByTagName('categories')->item(0)->appendChild($cat);
    
    $sxml = simplexml_import_dom($dom);  // DOM -> SimpleXML
    return $sxml;
}

方案三:XPath(類 SQL 查詢、精準定位)

SimpleXML 與 DOM 均內建 XPath 支援。常用路徑語法:

XPath 表達式含義
//category文檔中所有 category 元素
/library/books根節點下的 books 子元素
/library/categories/category[@cid]帶 cid 屬性的 category
/library/categories/category[@cid='2']cid=2 的 category
/library/books/book[title='Apache 2']標題為 Apache 2 的 book

SimpleXML 中使用 XPath

<?php
$xml = simplexml_load_file('xml/library.xml');

foreach ($xml->xpath("/library/books/book") as $book) {
    $catList = [];
    foreach ($book->category as $catId) {
        // 反向查找分類名
        $cat = $xml->xpath("/library/categories/category[@cid='$catId']");
        $catList[] = (string)$cat[0];
    }
    // 輸出...
}
?>

DOM 中使用 XPath

$xpath = new DOMXPath($dom);
$books = $xpath->evaluate("/library/books/book[title='Apache 2']");
foreach ($books as $book) {
    // $book 是 DOMElement
}

三方案對比與選型建議

維度DOMSimpleXMLXPath
學習曲線較陡(冗長 API)平緩(直觀屬性訪問)中等(需學路徑語法)
內存佔用高(整棵樹載入)高(同 DOM)依賴宿主解析器
修改能力完整(增刪改查)受限(需轉 DOM)唯讀查詢
查詢靈活性需手動遍歷需手動遍歷極強(複雜條件一行搞定)
推薦場景需修改結構、大文檔、跨語言純讀取、結構固定、快速開發複雜條件篩選、報表統計

最佳實踐組合拳

  1. 讀取配置/數據饋送:simplexml_load_file() + XPath 精準提取
  2. 生成/修改 XML 文件:DOMDocument 構建樹 → save() 持久化
  3. 大文檔流式處理:XMLReader(拉式解析)+ XMLWriter,避免全量載入內存
  4. 命名空間處理:DOMXPath::registerNamespace() 或 SimpleXMLElement::registerXPathNamespace()

結語

PHP 5+ 提供了成熟的 XML 工具鏈:DOM 兼容標準、SimpleXML 開發高效、XPath 查詢強大。實際項目中常「SimpleXML 讀取 + XPath 篩選 + DOM 修改」組合使用。掌握三者優缺點,可根據業務場景靈活選型,避免過度設計或性能瓶頸。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved