Home >Backend Development >PHP Tutorial >How Do I Access Hyphenated Node Names in SimpleXML?

How Do I Access Hyphenated Node Names in SimpleXML?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-12-08 22:23:16183browse

How Do I Access Hyphenated Node Names in SimpleXML?

Reading Node with a Hyphenated Name in SimpleXML

When attempting to read an XML node with a hyphenated name using SimpleXML, the standard dot notation may not work as expected. The code provided illustrates this issue and results in an undefined constant error.

To resolve this specific issue, use array notation to access the node with a hyphenated name, as demonstrated by this corrected code:

$officeXML->{'document-meta'}

This syntax is necessary for Element nodes, while Attribute nodes within @attributes can be accessed directly using array notation. For example:

echo $root->{'hyphenated-element'}['hyphenated-attribute']; // prints "bar"

For a comprehensive overview of SimpleXML basics, please refer to the SimpleXml Basics section in the Manual.

The above is the detailed content of How Do I Access Hyphenated Node Names in SimpleXML?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn