xref: /PHP-8.2/ext/simplexml/tests/007.phpt (revision 7f2f0c00)
1--TEST--
2SimpleXML: Attributes
3--EXTENSIONS--
4simplexml
5--FILE--
6<?php
7
8$xml =<<<EOF
9<?xml version='1.0'?>
10<!DOCTYPE sxe SYSTEM "notfound.dtd">
11<sxe id="elem1">
12 <elem1 attr1='first'>
13  <!-- comment -->
14  <elem2>
15   <elem3>
16    <elem4>
17     <?test processing instruction ?>
18    </elem4>
19   </elem3>
20  </elem2>
21 </elem1>
22</sxe>
23EOF;
24
25$sxe = simplexml_load_string($xml);
26
27echo "===Property===\n";
28var_dump($sxe->elem1);
29echo "===Array===\n";
30var_dump($sxe['id']);
31var_dump($sxe->elem1['attr1']);
32echo "===Set===\n";
33$sxe['id'] = "Changed1";
34var_dump($sxe['id']);
35$sxe->elem1['attr1'] = 12;
36var_dump($sxe->elem1['attr1']);
37echo "===Unset===\n";
38unset($sxe['id']);
39var_dump($sxe['id']);
40unset($sxe->elem1['attr1']);
41var_dump($sxe->elem1['attr1']);
42echo "===Misc.===\n";
43$a = 4;
44var_dump($a);
45$dummy = $sxe->elem1[$a];
46var_dump($a);
47?>
48--EXPECTF--
49===Property===
50object(SimpleXMLElement)#%d (3) {
51  ["@attributes"]=>
52  array(1) {
53    ["attr1"]=>
54    string(5) "first"
55  }
56  ["comment"]=>
57  object(SimpleXMLElement)#%d (0) {
58  }
59  ["elem2"]=>
60  object(SimpleXMLElement)#%d (1) {
61    ["elem3"]=>
62    object(SimpleXMLElement)#%d (1) {
63      ["elem4"]=>
64      object(SimpleXMLElement)#%d (1) {
65        ["test"]=>
66        object(SimpleXMLElement)#%d (0) {
67        }
68      }
69    }
70  }
71}
72===Array===
73object(SimpleXMLElement)#%d (1) {
74  [0]=>
75  string(5) "elem1"
76}
77object(SimpleXMLElement)#%d (1) {
78  [0]=>
79  string(5) "first"
80}
81===Set===
82object(SimpleXMLElement)#%d (1) {
83  [0]=>
84  string(8) "Changed1"
85}
86object(SimpleXMLElement)#%d (1) {
87  [0]=>
88  string(2) "12"
89}
90===Unset===
91NULL
92NULL
93===Misc.===
94int(4)
95int(4)
96