PHP getNamespace() Function

Definition and Usage

The getNamespace() function retrieves the namespaces used in the XML document.

If successful, the function returns an array of namespaces (with associated URLs). If failed, it returns false.

Syntax

class SimpleXMLElement
{
string getNamespace()recursive)
}
Parameter Description
recursive Optional. Specifies whether to return all namespaces used in the parent and 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->getNamespaces(););
?>

Output similar to:

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