DOMDocument操作工具类的集合

v3.2.1 2023-07-27 14:40 UTC

This package is auto-updated.

Last update: 2024-08-27 16:50:15 UTC


README

Latest Stable Version Latest Unstable Version License

Total Downloads Monthly Downloads Daily Downloads

Documentation

用于操作PHP DOM对象的工具类集合

需求

subjective-php\dom 需要 PHP 7.0(或更高版本)。

##Composer 要将库添加为本地项目依赖项,请使用 Composer!只需将 subjective-php/dom 添加到项目中的 composer.json 文件中,例如

composer require subjective-php/dom

##联系方式 开发者可以通过以下方式联系

##运行单元测试 通过对代码进行检出,在您的PATH中运行 Composer

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>

使用XPath构建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>