chadicus / dom
v3.2.1
2023-07-27 14:40 UTC
Requires
- php: ^7.0 || ^8.0
- ext-dom: *
Requires (Dev)
- phpunit/phpunit: >=6.5
- squizlabs/php_codesniffer: ^3.2
README
PHP DOM 对象的实用类集合
需求
subjective-php\dom 需要 PHP 7.0(或更高版本)。
##Composer 要将库作为本地、项目级依赖项添加,请使用 Composer!只需将 subjective-php/dom
依赖项添加到项目的 composer.json
文件中,例如
composer require subjective-php/dom
##联系 开发者可通过以下方式联系
##运行单元测试 在代码检出后,将 Composer 添加到您的 PATH 中并运行
composer install ./vendor/bin/phpunit
示例
将 XML 文档转换为数组
<?php use SubjectivePHP\Util; $xml = <<<XML <?xml version="1.0"?> <catalog> <book id="bk101"> <author>Gambardella, Matthew</author> <title>XML Developer's Guide</title> <genre>Computer</genre> <price>44.95</price> <publish_date>2000-10-01</publish_date> <description>An in-depth look at creating applications with XML.</description> </book> <book id="bk102"> <author>Ralls, Kim</author> <title>Midnight Rain</title> <genre>Fantasy</genre> <price>5.95</price> <publish_date>2000-12-16</publish_date> <description>A former architect battles corporate zombies, an evil sorceress, and her own childhood to become queen of the world.</description> </book> XML; $document = new \DOMDocument(); $document->loadXml($xml); $array = Util\DOMDocument::toArray($document); var_export($array);
输出将类似
array (
'catalog' =>
array (
'book' =>
array (
0 =>
array (
'@id' => 'bk101',
'author' => 'Gambardella, Matthew',
'title' => 'XML Developer\'s Guide',
'genre' => 'Computer',
'price' => '44.95',
'publish_date' => '2000-10-01',
'description' => 'An in-depth look at creating applications with XML.',
),
1 =>
array (
'@id' => 'bk102',
'author' => 'Ralls, Kim',
'title' => 'Midnight Rain',
'genre' => 'Fantasy',
'price' => '5.95',
'publish_date' => '2000-12-16',
'description' => 'A former architect battles corporate zombies, an evil sorceress, and her own childhood to become queen of the world.',
),
),
),
)
将数组转换为 XML
<?php use SubjectivePHP\Util; $catalog = [ 'book' => [ [ '@id' => '58339e95d52d9', 'author' => 'Corets, Eva', 'title' => 'The Sundered Grail', 'genre' => 'Fantasy', 'price' => 5.95, 'published' => 1000094400, 'description' => "The two daughters of Maeve, half-sisters, battle one another for control of England. Sequel to Oberon's Legacy.", ], [ '@id' => '58339e95d530e', 'author' => 'Randall, Cynthia', 'title' => 'Lover Birds', 'genre' => 'Romance', 'price' => 4.95, 'published' => 967867200, 'description' => 'When Carla meets Paul at an ornithology conference, tempers fly as feathers get ruffled.', ], ], ]; $document = Util\DOMDocument::fromArray(['catalog' => $catalog]); $document->formatOutput = true; echo $document->saveXml();
输出
<?xml version="1.0"?>
<catalog>
<book id="58339e95d52d9">
<author>Corets, Eva</author>
<title>The Sundered Grail</title>
<genre>Fantasy</genre>
<price>5.95</price>
<published>1000094400</published>
<description>The two daughters of Maeve, half-sisters, battle one another for control of England. Sequel to Oberon's Legacy.</description>
</book>
<book id="58339e95d530e">
<author>Randall, Cynthia</author>
<title>Lover Birds</title>
<genre>Romance</genre>
<price>4.95</price>
<published>967867200</published>
<description>When Carla meets Paul at an ornithology conference, tempers fly as feathers get ruffled.</description>
</book>
</catalog>
使用 xpaths 构建 XML 文档
<?php use SubjectivePHP\Util; $document = new DOMDocument(); $document->formatOutput = true; Util\DOMDocument::addXPath($document, "/catalog/book[@id='58339e95d530e']/title", 'Lover Birds'); Util\DOMDocument::addXPath($document, '/catalog/book[@id="58339e95d530e"]/price', 4.95); Util\DOMDocument::addXPath($document, '/catalog/book[@id="58339e95d52d9"]/title', 'The Sundered Grail'); Util\DOMDocument::addXPath($document, '/catalog/book[@id="58339e95d52d9"]/genre', 'Fantasy'); echo $document->saveXml();
输出
<?xml version="1.0"?>
<catalog>
<book id="58339e95d530e">
<title>Lover Birds</title>
<price>4.95</price>
</book>
<book id="58339e95d52d9">
<title>The Sundered Grail</title>
<genre>Fantasy</genre>
</book>
</catalog>