Tag Archives: SimpleXML

SimpleXML

SimpleXML is an extension that allows us to easily manipulate and get XML data.

SimpleXML provides the ability to iterate over items in an XML document, as well as
access items within it as if they were object properties

SimpleXML provides an easy way of getting an element’s name, attributes and textual content if you know the XML document’s structure or layout.

SimpleXML turns an XML document into a data structure you can iterate through like a collection of arrays and objects.

  • SimpleXML allows removal of attributes.
  • SimpleXML allows addition of new attributes.
  • SimpleXML allows removal of nodes.
  • SimpleXML allows addition of new nodes.

xpath()

It runs an XPath query on XML data

  • XPath is a major element in the XSLT standard.
  • XPath can be used to navigate through elements and attributes in an XML document.
  • XPath stands for XML Path Language
  • XPath uses “path like” syntax to identify and navigate nodes in an XML document
  • XPath contains over 200 built-in functions
  • XPath is a major element in the XSLT standard
  • XPath is a W3C recommendation

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>

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>


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>


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>


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>


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>