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
Release: 2024-12-08 22:23:16
Original
186 people have browsed it

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'}
Copy after login

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"
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template