Namespace are used in XML document to avoid conflict of name used by different XML developers. For example name element can have name of the student or it can have name of the fruits in basket.

We will discuss more on how to get namespace details by using PHP. Here is the example to get namespace from our XML file. We have used one more sample file which used namespace in its elements.


$str_xml = <<<XML
<?xml version='1.0' standalone='yes'?>
<b:details xmlns:b="">

<o:address xmlns:o="">
  <o:street>Great Wall</o:street>

<r:address xmlns:r="">
  <r:street>Garden street</r:street>
  <r:city>New Work</r:city>

We will use PHP script to get the namespaces from above XML data. Here it is
require "xml-sample2.php";
$main1 = new SimpleXMLElement($str_xml);
The output is
array(3) { ["b"]=> string(25) "" ["o"]=> string(26) "" ["r"]=> string(26) "" }
By using recursive to TRUE we will get name space of total documents including all child nodes. By making it FALSE it will return only root node namespaces.

Now change this line
We will get only document root namespace
array(1) { ["b"]=> string(25) "" }

XML XML Children

Subscribe to our YouTube Channel here


* indicates required
Subscribe to plus2net

    Post your comments , suggestion , error , requirements etc here

    PHP video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 All rights reserved worldwide Privacy Policy Disclaimer