xref: /PHP-7.2/ext/simplexml/tests/007.phpt (revision 17ccbeec)
1--TEST--
2SimpleXML: Attributes
3--SKIPIF--
4<?php if (!extension_loaded("simplexml")) print "skip"; ?>
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===Done===
49--EXPECTF--
50===Property===
51object(SimpleXMLElement)#%d (3) {
52  ["@attributes"]=>
53  array(1) {
54    ["attr1"]=>
55    string(5) "first"
56  }
57  ["comment"]=>
58  object(SimpleXMLElement)#%d (0) {
59  }
60  ["elem2"]=>
61  object(SimpleXMLElement)#%d (1) {
62    ["elem3"]=>
63    object(SimpleXMLElement)#%d (1) {
64      ["elem4"]=>
65      object(SimpleXMLElement)#%d (1) {
66        ["test"]=>
67        object(SimpleXMLElement)#%d (0) {
68        }
69      }
70    }
71  }
72}
73===Array===
74object(SimpleXMLElement)#%d (1) {
75  [0]=>
76  string(5) "elem1"
77}
78object(SimpleXMLElement)#%d (1) {
79  [0]=>
80  string(5) "first"
81}
82===Set===
83object(SimpleXMLElement)#%d (1) {
84  [0]=>
85  string(8) "Changed1"
86}
87object(SimpleXMLElement)#%d (1) {
88  [0]=>
89  string(2) "12"
90}
91===Unset===
92NULL
93NULL
94===Misc.===
95int(4)
96int(4)
97===Done===
98