Home >Backend Development >PHP Tutorial >How to Access Hyphenated XML Nodes Using SimpleXML?
Reading XML nodes withhyphenated names using SimpleXML can be tricky. The native SimpleXML library expects nodes to be specifically prefixed with a colon for namespaces (and converted to all-uppercase letters) and treated as a child to the currently loaded XML root element.
For instance, to access the document-meta node in the provided XML:
<?xml version="1.0" encoding="UTF-8"?> <gnm:Workbook xmlns:gnm="http://www.gnumeric.org/v10.dtd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.gnumeric.org/v9.xsd"> <office:document-meta xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:meta="urn:oasis:names:tc:opendocument:xmlns:meta:1.0" office:version="1.1"> <dc:creator>Mark Baker</dc:creator> <dc:date>2010-09-01T22:49:33Z</dc:date> <meta:creation-date>2010-09-01T22:48:39Z</meta:creation-date> <meta:editing-cycles>4</meta:editing-cycles> <meta:editing-duration>PT00H04M20S</meta:editing-duration> <meta:generator>OpenOffice.org/3.1$Win32 OpenOffice.org_project/310m11$Build-9399</meta:generator> </office:meta> </office:document-meta> </gnm:Workbook>
Instead of using $xml->children($namespacesMeta['office']), use:
$officeXML = $xml->children($namespacesMeta['office'])->{’document-meta’};
This will access the document-meta node and its children accordingly.
Note that this only applies to accessing hyphenated XML elements. For attribute nodes, they can be accessed normally using the @attributes array notation.
For additional clarity on accessing elements with special names in XML using SimpleXML, refer to the documentation at:
[SimpleXML Basics](https://www.php.net/manual/en/simplexml.intro-example.php)
The above is the detailed content of How to Access Hyphenated XML Nodes Using SimpleXML?. For more information, please follow other related articles on the PHP Chinese website!