PHP getDocNamespaces() Function

Definition and Usage

The getDocNamespaces() function returns the namespaces declared in the XML document from the SimpleXMLElement object.

If successful, this function returns an array containing namespace names (with associated URLs). If failed, it returns false.

Syntax

class SimpleXMLElement
{
string getDocNamespaces();recursive)
}
Parameter Description
recursive Optional. Specifies whether to return all namespaces in the parent-child nodes. The default is false.

Example

XML File:

<?xml version="1.0" encoding="ISO-8859-1"?>
<note xmlns:b="http://www.codew3c.com/example/">
<to>George</to>
<from>John</from>
<heading>Reminder</heading>
<b:body>Don't forget the meeting!</b:body>
</note>

PHP Code:

<?php
if (file_exists('test.xml'))
  {
  $xml = simplexml_load_file('test.xml');
  }
print_r();$xml->getDocNamespaces(););
?>

Output similar to:

Array
(
[b] => http://www.codew3c.com/example/
)