具有连字符名称的简单XML读取节点

2022-08-30 19:06:01

我有以下 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" xmlns:ooo="http://openoffice.org/2004/office" 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>

我试图阅读office:document-meta节点来提取它下面的各种元素(dc:creator,meta:creation-date等)。

下面的代码:

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

给我:

object(SimpleXMLElement)[91]
  public 'document-meta' => 
    object(SimpleXMLElement)[93]
      public '@attributes' => 
        array
          'version' => string '1.1' (length=3)
      public 'meta' => 
        object(SimpleXMLElement)[94]

但是如果我尝试使用以下内容读取 document-meta 元素:

$xml = simplexml_load_string($gFileData);
$namespacesMeta = $xml->getNamespaces(true);
$officeXML = $xml->children($namespacesMeta['office']);
$docMeta = $officeXML->document-meta;
var_dump($docMeta);
echo '<hr />';

我得到

Notice: Use of undefined constant meta - assumed 'meta' in /usr/local/apache/htdocsNewDev/PHPExcel/Classes/PHPExcel/Reader/Gnumeric.php on line 273
int 0

我假设 SimpleXML 试图从$officeXML中提取一个不存在的节点“document”,然后减去(不存在的)常量 “meta” 的值,从而强制整数 0 结果而不是 document-meta 节点。

有没有办法使用SimpleXML解决这个问题,或者我会被迫使用XMLReader重写?任何帮助赞赏。


答案 1

你的假设是正确的。用

$officeXML->{'document-meta'}

使其正常工作。

请注意,上述内容适用于元素节点。属性节点(转储 SimpleXmlElement 时@attributes属性中的节点)在连字符连接时不需要访问任何特殊语法。它们通常可以通过数组表示法访问,例如

$xml = <<< XML
<root>
    <hyphenated-element hyphenated-attribute="bar">foo</hyphenated-element>
</root>
XML;
$root = new SimpleXMLElement($xml);
echo $root->{'hyphenated-element'}; // prints "foo"
echo $root->{'hyphenated-element'}['hyphenated-attribute']; // prints "bar"

有关更多示例,请参阅手册中的 SimpleXml 基础知识


答案 2

我认为最好的方法是强制转换为数组:

请考虑以下 XML:

<subscribe hello-world="yolo">
    <callback-url>example url</callback-url>
</subscribe>

您可以使用强制转换访问成员,包括属性:

<?php
$xml = (array) simplexml_load_string($input);
$callback = $xml["callback-url"];
$attribute = $xml['@attributes']['hello-world'];

它使一切变得更容易。希望我有帮助。


推荐