xref: /PHP-8.2/ext/xmlreader/tests/003.phpt (revision 74859783)
1--TEST--
2XMLReader: libxml2 XML Reader, attributes test
3--EXTENSIONS--
4xmlreader
5--FILE--
6<?php
7$filename = __DIR__ . '/_003.xml';
8
9$xmlstring = '<?xml version="1.0" encoding="UTF-8"?>
10<books><book num="1" idx="2">book1</book></books>';
11file_put_contents($filename, $xmlstring);
12
13$reader = new XMLReader();
14if (!$reader->open($filename)) {
15    exit();
16}
17
18// Only go through
19while ($reader->read()) {
20    if ($reader->nodeType != XMLREADER::END_ELEMENT) {
21        if ($reader->nodeType == XMLREADER::ELEMENT && $reader->hasAttributes) {
22            $attr = $reader->moveToFirstAttribute();
23            echo $reader->name . ": ";
24            echo $reader->value . "\n";
25
26            if ($reader->getAttribute($reader->name) == $reader->value) {
27                echo "1st attr (num) failed\n";
28            }
29
30
31            $attr = $reader->moveToNextAttribute();
32            echo $reader->name . ": ";
33            echo $reader->value . "\n";
34
35            if ($reader->getAttribute($reader->name) == $reader->value) {
36                echo "2nd attr (idx) failed\n";
37            }
38
39            // Named attribute
40            $attr = $reader->moveToAttribute('num');
41            echo $reader->name . ": ";
42            echo $reader->value . "\n";
43
44            if ($reader->getAttribute('num') == $reader->value) {
45                echo "attr num failed\n";
46            }
47
48            $attr = $reader->moveToAttribute('idx');
49            echo $reader->name . ": ";
50            echo $reader->value . "\n";
51
52            if ($reader->getAttribute('idx') == $reader->value) {
53                echo "attr idx failed\n";
54            }
55
56            // Numeric positions of attributes
57            $attr = $reader->moveToAttributeNo(0);
58            echo $reader->name . ": ";
59            echo $reader->value . "\n";
60
61            if ($reader->getAttributeNo(0) == $reader->value) {
62                echo "attr 0 failed\n";
63            }
64
65            $attr = $reader->moveToAttributeNo(1);
66            echo $reader->name . ": ";
67            echo $reader->value . "\n";
68
69            var_dump($reader->moveToAttributeNo(20));
70            var_dump($reader->moveToAttribute('missing-attribute'));
71            try {
72                $reader->moveToAttribute('');
73            } catch (ValueError $exception) {
74                echo $exception->getMessage() . "\n";
75            }
76        }
77    }
78}
79$reader->close();
80unlink($filename);
81?>
82--EXPECT--
83num: 1
84idx: 2
85num: 1
86idx: 2
87num: 1
88idx: 2
89bool(false)
90bool(false)
91XMLReader::moveToAttribute(): Argument #1 ($name) cannot be empty
92