Tag Archives: SimpleXML

xpath()

It runs an XPath query on XML data

Example

<?php
$xml = simplexml_load_file(“test.xml”);
$xml->registerXPathNamespace(“msg”,”http://www.phpcodez.com”);
$result = $xml->xpath(“msg:web”);
?>

XML

<?xml version=”1.0″ encoding=”ISO-8859-1″?>
<web>
<server ver=”5″>Apache</server>
<lan>PHP</lan>
<client>Javascript</client>
</web>

Share

simplexml_load_string()

It returns  SimpleXMLElement object from an XML string

Example

<?php
$xmlstring = ‘<?xml version=”1.0″ encoding=”ISO-8859-1″?><lan>PHP</lan>’;
$xml = simplexml_load_string($xmlstring);
print_r($xml);
?>

Output

SimpleXMLElement Object ( [0] => PHP )

Share

registerXPathNamespace()

It creates a namespace context for the next XPath query

Example

<?php
$xml = simplexml_load_file(“test.xml”);
$xml->registerXPathNamespace(“msg”,”http://www.phpcodez.com”);
$result = $xml->xpath(“msg:web”);
?>

XML

<?xml version=”1.0″ encoding=”ISO-8859-1″?>
<web>
<server ver=”5″>Apache</server>
<lan>PHP</lan>
<client>Javascript</client>
</web>


Share

getNamespace()

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>


Share

getName()

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>


Share

getDocNamespaces()

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>


Share

children()

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>


Share

attributes()

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>

Share