Home > Backend Development > PHP Tutorial > How to Access Hyphenated XML Node Names Using SimpleXML in PHP?

How to Access Hyphenated XML Node Names Using SimpleXML in PHP?

Patricia Arquette
Release: 2024-12-14 04:52:10
Original
944 people have browsed it

How to Access Hyphenated XML Node Names Using SimpleXML in PHP?

Reading XML Node with Hyphenated Name Using SimpleXML

In PHP, using SimpleXML to extract data from XML documents becomes challenging when you encounter elements with hyphenated names. Let's delve into this problem and find a solution.

Consider the following XML:

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

To read the document-meta element and its child elements, consider the following initial code:

$xml = simplexml_load_string($gFileData);
$namespacesMeta = $xml->getNamespaces(true);
$officeXML = $xml->children($namespacesMeta['office']);
var_dump($officeXML);

echo '<hr />';
Copy after login

This code dumps all immediate child elements of the office element, which includes document-meta. However, trying to access document-meta directly using $officeXML->document-meta fails and returns int 0.

The reason for this behavior lies in SimpleXML's handling of hyphenated names. By default, it converts hyphenated element names to camelCase. In this case, "document-meta" becomes "documentMeta". To access this element, use the following syntax:

$docMeta = $officeXML->{'document-meta'};
var_dump($docMeta);
Copy after login

By using curly braces and string notation, you can access the actual element name without SimpleXML's default case conversion. This will successfully dump the document-meta element.

Note: This syntax applies only to element nodes. Attribute nodes within @attributes can be accessed directly using array notation, regardless of their hyphenation.

The above is the detailed content of How to Access Hyphenated XML Node Names Using SimpleXML in PHP?. 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