php - SimpleXML Reading node with a hyphenated name -


i have following 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> 

and trying read office:document-meta node extractthe various elements below (dc:creator, meta:creation-date, etc.)

the following code:

$xml = simplexml_load_string($gfiledata); $namespacesmeta = $xml->getnamespaces(true); $officexml = $xml->children($namespacesmeta['office']); var_dump($officexml); echo '<hr />'; 

gives me:

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

but if try read document-meta element using:

$xml = simplexml_load_string($gfiledata); $namespacesmeta = $xml->getnamespaces(true); $officexml = $xml->children($namespacesmeta['office']); $docmeta = $officexml->document-meta; var_dump($docmeta); echo '<hr />'; 

i get

notice: use of undefined constant meta - assumed 'meta' in /usr/local/apache/htdocsnewdev/phpexcel/classes/phpexcel/reader/gnumeric.php on line 273 int 0 

i assume simplexml trying extract non-existent node "document" $officexml, subtract value of (non-existent) constant "meta", resulting in forcing integer 0 result rather document-meta node.

is there way resolve using simplexml, or forced rewrite using xmlreader? appreciated.

your assumption correct. use

$officexml->{'document-meta'} 

to make work.

please note above applies element nodes. attribute nodes (those within @attributes property when dumping simplexmlelement) not require special syntax accessed when hyphenated. regularly accessible via array notation, e.g.

$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" 

see simplexml basics in manual further examples.


Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -

How to provide Authorization & Authentication using Asp.net, C#? -