W3docs

getName()

SimpleXML is a PHP extension that provides a simple and easy-to-use API for working with XML documents. The SimpleXMLElement::getName() function is one of the

Introduction

SimpleXMLElement::getName() returns the tag name of the XML element you call it on — not its text content, not a class name. It is part of SimpleXML, the lightweight PHP extension for reading and editing XML with plain object syntax.

This page covers the method signature, a runnable example, how getName() behaves with namespaces and loops, and the gotchas that trip people up.

Syntax

public SimpleXMLElement::getName(): string

The method takes no arguments and returns a string holding the local name of the element. The leading < and >, any attributes, and the element's child nodes are all ignored — you get just the tag name.

Basic example

php— editable, runs on the server

$xml points at the root <book> element, so $xml->getName() returns book. Stepping into the title child and calling getName() returns title. Note that getName() gives you the tag name, while (string) $xml->title would give you the text content, PHP Basics.

Why use it

You usually know the element names in advance, so why ask for them at runtime? getName() is most useful when you are iterating over children whose names you don't control — for example, walking a mixed list of records or building a generic XML-to-array converter.

<?php

$xml = new SimpleXMLElement(
    '<library><book>PHP</book><magazine>Wired</magazine><book>SQL</book></library>'
);

foreach ($xml->children() as $child) {
    echo $child->getName() . ': ' . $child . "\n";
}
// book: PHP
// magazine: Wired
// book: SQL

Here the loop doesn't hard-code book or magazine; getName() tells you what each node is as you visit it. Pair it with children() to traverse a tree and attributes() to read each element's attributes.

Namespaces

For an element in an XML namespace, getName() returns the local name without the prefix. The example below uses getName() together with getNamespaces() to recover the full context.

<?php

$xml = new SimpleXMLElement(
    '<root xmlns:h="http://example.com/html"><h:td>Cell</h:td></root>'
);

$cell = $xml->children('http://example.com/html')[0];

echo $cell->getName();                          // td  (prefix stripped)
echo "\n";
echo array_key_first($cell->getNamespaces());   // h

If you only call getName() you lose the h: prefix, so query the namespace separately when the prefix matters.

Common gotchas

  • It is a method, not a property. Write $el->getName() with parentheses, not $el->getName.
  • It returns the tag, not the value. Use (string) $el or $el->__toString() for the text inside the element.
  • Empty selections. Accessing a child that doesn't exist still yields a SimpleXMLElement, and getName() on it returns an empty string rather than throwing — check with count() first if that matters.
  • Loading from a string/file. Build elements with simplexml_load_string() or simplexml_load_file(); both return objects on which getName() works the same way.

Conclusion

SimpleXMLElement::getName() returns the tag name of an XML element using clean object syntax. It shines when you process XML whose structure you don't know ahead of time — combine it with children(), attributes(), and asXML() to read and rewrite documents. For the bigger picture of parsing XML in PHP, start with the SimpleXML overview.

Practice

Practice
What does SimpleXMLElement::getName() return?
What does SimpleXMLElement::getName() return?
Was this page helpful?