Online
 
Sunday, 23 November 2008
 
 

SIMPLEXML | Print |  E-Mail
 
The SimpleXML extension, enabled by default in PHP 5, is the easiest way
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.

This entry was posted on . You can follow any responses to this entry through the RSS 2.0 feed. You can leave a comment.
Users' Comments (0)

Comment an article
  Name
  E-mail
   Title
Available characters: 4000
 Notify me of follow-up comments
This image contains a scrambled text, it is using a combination of colors, font size, background, angle in order to disallow computer to automate reading. You will have to reproduce it to post on my homepage
Enter what you see:

No comment posted

Jumbo Coklat
 
Top! Top!