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!