Reading XML Nodes with Hyphenated Names in SimpleXML
SimpleXML, a PHP library for parsing XML documents, can encounter difficulties when reading nodes with hyphenated names. For instance, consider the XML below:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
Attempting to read the office:document-meta node using SimpleXML's children() method results in the error "Use of undefined constant meta - assumed 'meta'". This is because SimpleXML interprets the hyphen as a subtraction operator.
Solution
To overcome this issue, use curly braces instead of the hyphen:
1 |
|
This syntax allows you to access the document-meta node.
Accessing Hyphenated Attributes
While hyphenated element names require the curly brace syntax, hyphenated attributes can be accessed using regular array notation:
1 2 |
|
Refer to the SimpleXML Basics documentation for additional examples.
The above is the detailed content of How Can I Access Hyphenated XML Node Names Using SimpleXML?. For more information, please follow other related articles on the PHP Chinese website!