我有以下XML:<?xml version="1.0" encoding="UTF-8"?><gnm:Workbook xmlns:gnm="http://www.gnumeric.org/v10.dtd" xmlns:xsi="
a-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/offi
ce" 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:create-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]但是,如果我尝试使用以下方法读取文档-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 273int 0我假设SimpleXML试图从$office eXML中提取一个不存在的节点“文档”,然后减去(不存在)常量“meta”的值,从而导致强制整数0结果而不是文档元节点。是否有一种使用SimpleXML解决此问题的方法,或者我将被迫使用XMLReader重写?任何帮助都很感激。
2 回答
![?](http://img1.sycdn.imooc.com/54584f3100019e9702200220-100-100.jpg)
繁花不似锦
TA贡献1851条经验 获得超4个赞
你的假设是正确的。使用
$officeXML->{'document-meta'}
让它发挥作用。
请注意,上述内容适用于元素节点。属性节点(在转储SimpleXmlElement时在@Attribute属性中)不需要在连字符时访问任何特殊语法。它们可以通过数组符号定期访问。
$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"
![?](http://img1.sycdn.imooc.com/545847f50001126402200220-100-100.jpg)
弑天下
TA贡献1818条经验 获得超8个赞
{document-meta}
{'document-meta'}
- 2 回答
- 0 关注
- 551 浏览
添加回答
举报
0/150
提交
取消