tld
there is no subjectsymbols]]>
END;
$sxml = simplexml_load_string($str);
print_r($sxml);
?>
The output is like this:
SimpleXMLElement Object
(
[email] => SimpleXMLElement Object
(
[from] => nowhere@notadomain.tld
[to] => unknown@unknown.tld
[subject] => there is no subject
[body] => SimpleXMLElement Object
( )
)
)
So now you can ask how to access each of these properties individually. You can
access each of them like an object. For example, $sxml->email[0] returns the first
email object. To access the from element under this email, you can use the following
code like:
echo $sxml->email[0]->from
Cooking XML with OOP
[ 198 ]
So, each object, unless available more than once, can be accessed just by its name.
Otherwise you have to access them like a collection. For example, if you have
multiple elements, you can access each of them using a foreach loop:
foreach ($sxml->email as $email)
echo $email->from;
Accessing Attributes
As we saw in the previous example, XML nodes may have attributes. Remember the
example document with class, age, and title? Now you can easily access these
attributes using SimpleXML API.
Pages:
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214