<?php
declare(strict_types=1);
$cases = [];
function add_case(array &$cases, string $name, mixed $expected, mixed $actual): void {
$cases[] = [
'name' => $name,
'passed' => ($actual === $expected),
'expected' => $expected,
'actual' => $actual,
];
}
$noteXml = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n"
. "<note>\n"
. "<to>Tove</to>\n"
. " <from>Jani</from>\n"
. " <heading>Reminder</heading>\n"
. " <body>Don't forget me this weekend!</body>\n"
. "</note>\n";
$tmpFile = tempnam(sys_get_temp_dir(), 'sxml_note_');
if ($tmpFile === false) {
throw new RuntimeException('Failed to create temp file');
}
try {
file_put_contents($tmpFile, $noteXml);
$xml = simplexml_load_file($tmpFile);
if ($xml === false) {
throw new RuntimeException('Failed to load XML');
}
// Case 1: simplexml_load_file() structure and root element name
$keys = array_keys(get_object_vars($xml));
add_case(
$cases,
'simplexml_load_and_structure',
[
'class' => SimpleXMLElement::class,
'root' => 'note',
'keys' => ['to', 'from', 'heading', 'body'],
],
[
'class' => get_class($xml),
'root' => $xml->getName(),
'keys' => $keys,
]
);
// Case 2: Direct property access for each child element
add_case(
$cases,
'simplexml_direct_property_access',
['Tove', 'Jani', 'Reminder', "Don't forget me this weekend!"],
[(string) $xml->to, (string) $xml->from, (string) $xml->heading, (string) $xml->body]
);
// Case 3: Iterating children() with getName() and string cast
$childrenOut = [$xml->getName()];
foreach ($xml->children() as $child) {
$childrenOut[] = $child->getName() . ': ' . (string) $child;
}
add_case(
$cases,
'simplexml_children_iteration',
[
'note',
'to: Tove',
'from: Jani',
'heading: Reminder',
"body: Don't forget me this weekend!",
],
$childrenOut
);
} finally {
if (is_file($tmpFile)) {
unlink($tmpFile);
}
}
// Case 4: Attributes as associative array, repeated sibling elements, and editing
$catalogXml = <<<'XML'
<?xml version="1.0" encoding="UTF-8"?>
<catalog>
<book id="b1" lang="en">PHP Basics</book>
<book id="b2" lang="zh">XML Guide</book>
</catalog>
XML;
$cat = simplexml_load_string($catalogXml);
$cat->book[0] = 'PHP 8.4 Basics';
$cat->book[1]['lang'] = 'zh-CN';
$updatedXml = $cat->asXML();
add_case(
$cases,
'simplexml_attributes_repeated_and_edit',
[
'book_count' => 2,
'first_id' => 'b1',
'second_title' => 'XML Guide',
'edited_first_title' => 'PHP 8.4 Basics',
'edited_second_lang' => 'zh-CN',
'persisted_in_asxml' => true,
],
[
'book_count' => count($cat->book),
'first_id' => (string) $cat->book[0]['id'],
'second_title' => (string) $cat->book[1],
'edited_first_title' => (string) $cat->book[0],
'edited_second_lang' => (string) $cat->book[1]['lang'],
'persisted_in_asxml' => (str_contains($updatedXml, 'PHP 8.4 Basics') && str_contains($updatedXml, 'lang="zh-CN"')),
]
);
echo json_encode(['version' => PHP_VERSION, 'cases' => $cases], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) . "\n";
本例需在 Windows x64、PHP 8.4 本地 CLI 环境。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Windows x64、PHP 8.4 本地 CLI 环境。
入口文件:projects/php-xml-simplexml-lab/explore-xml-simplexml.php
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-xml-simplexml.php,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、PHP 8.4 本地 CLI 环境。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联95章) · 含当前章节或实例 · mbskycom202612a1232.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a1317.zip