It returns the namespaces from XML data
Example
<?php
$xml = simplexml_load_file(‘test.xml’);
echo “<pre>”;
print_r($xml->getNamespace());
?>
Output
Array
(
[b] => http://www.phpcodez.come/
)
XML
<?xml version=”1.0″ encoding=”ISO-8859-1″?>
<web xmlns:b=”http://www.phpcodez.com/”>
<server ver=”5″>Apache</server>
<lan>PHP</lan>
<client>Javascript</client>
</web>
It returns the name of a SimpleXML element
Example
<?php
$xml = simplexml_load_file(‘test.xml’);
echo $xml->getName() ;
?>
Output
web
XML
<?xml version=”1.0″ encoding=”ISO-8859-1″?>
<web xmlns:b=”http://www.phpcodez.com/”>
<server ver=”5″>Apache</server>
<lan>PHP</lan>
<client>Javascript</client>
</web>
It returns the children of a specified node
Example
<?php
$xml = simplexml_load_file(‘test.xml’);
echo “<pre>”;
print_r($xml->getDocNamespaces());
?>
Output
Array
(
[b] => http://www.phpcodez.com/
XML
<?xml version=”1.0″ encoding=”ISO-8859-1″?>
<web xmlns:b=”http://www.phpcodez.com/”>
<server ver=”5″>Apache</server>
<lan>PHP</lan>
<client>Javascript</client>
</web>
It returns the children of a specified node
Example
<?php
$xml = simplexml_load_file(“test.xml”);
echo “<pre>”;
print_r($xml->children());
?>
Output
SimpleXMLElement Object
(
[server] => Apache
[lan] => PHP
[client] => Javascript
)
XML
<?xml version=”1.0″ encoding=”ISO-8859-1″?>
<web>
<server ver=”5″>Apache</server>
<lan>PHP</lan>
<client>Javascript</client>
</web>
It returns a SimpleXML element’s attributes
Example
<?php
$xml = simplexml_load_file(“test.xml”);
foreach($xml->server[0]->attributes() as $a => $b) {
echo $a;
}
?>
Output
ver
XML
<?xml version=”1.0″ encoding=”ISO-8859-1″?>
<web>
<server ver=”5″>Apache</server>
<lan>PHP</lan>
<client>Javascript</client>
</web>
It returns XML string from a SimpleXML element
Example
<?php
$xml = simplexml_load_file(‘test.xml’);
echo $xml->asXML();
?>
Output
Apache PHP Javascript
XML
<?xml version=”1.0″ encoding=”ISO-8859-1″?>
<web>
<server>Apache</server>
<lan>PHP</lan>
<client>Javascript</client>
</web>
It adds a child element the SimpleXML element
Example
<?php
$xml = simplexml_load_file(“test.xml”);
$xml->server[0]->addChild(“version”, “5”);
foreach ($xml->server->children() as $child)
{
echo $child . “<br />”;
}
?>
Output
5
XML
<?xml version=”1.0″ encoding=”ISO-8859-1″?>
<web>
<server>Apache</server>
<lan>PHP</lan>
<client>Javascript</client>
</web>
It adds an attribute to the SimpleXML element
Example
<?php
$xmlObj = simplexml_load_file(“test.xml”);
$xmlObj->client[0]->addAttribute(“ver”, “5”);
foreach($xmlObj->client[0]->attributes() as $a => $b) {
echo $a,’=”‘,$b,””</br>”;
}
?>
Output
ver=”5″
XML
<?xml version=”1.0″ encoding=”ISO-8859-1″?>
<web>
<server>Apache</server>
<lan>PHP</lan>
<client>Javascript</client>
</web>
It creates a new SimpleXMLElement object.
Example
<?php
class PHPCode {
function __construct() {
print “Inside constructor”;
}
}
$phpObj = new PHPCode();
?>
Output
Inside constructor
It wraps a string into new lines when it reaches a specific length.
Example
<?php
echo wordwrap(“It wraps a string into new lines when it reaches a specific length”,5);
?>
Output
It wraps a string into new lines when it reaches a specific length
Zend | Magento Certified PHP | eCommerce Architect