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