| SIMPLEXML | | Print | |
to work with XML. You don’t need to remember a difficult DOM API. You just
access the XML through a data structure representation. Here are its four
simple rules:
1. Properties denote element iterators.
2. Numeric indices denote elements.
3. Non-numeric indices denote attributes.
4. String conversion allows access to TEXT data.
By using these four rules, you can access all the data from an XML file.
You can create a SimpleXML object in any of three ways, as shown in this
example:
<?php
$sx1 = simplexml_load_file('example.xml');
$string = <<<XML
<?xml version='1.0'?>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>XML Example</title>
</head>
<body background="bg.png">
<p>
Moved to <a href="http://example.org/">example.org<a>.
</p>
<pre>
foo
</pre>
<p>
Moved to <a href="http://example.org/">example.org</a>.
</p>
</body>
</html>
XML;
$sx2 = simplexml_load_string($string);
$sx3 = simplexml_load_dom(new DomDocument());
?>
In the first method, simplexml_load_file() opens the specified file and
parses it into memory. In the second method, $string is created and passed to
the function simplexml_load_string(). In the third method,
simplexml_load_dom() imports a DomDocument created with the DOM functions in
PHP. In all three cases, a SimpleXML object is returned. The
simplexml_load_dom() function in SimpleXML extension has a brother in the
DOM extension, called dom_import_simplexml(). These related functions allow
you to share the same XML structure between both extensions. You can, for
example, modify simple documents with SimpleXML and more complicated
ones with DOM.
| Users' Comments (0) |
|
No comment posted






