note 66259 added to ref.simplexml

From: Date: Thu, 18 May 2006 01:21:45 +0000
Subject: note 66259 added to ref.simplexml
Groups: php.notes 
Request: Send a blank email to php-notes+get-111536@lists.php.net to get a copy of this message
SimpleXML handles namespaces, but it's not documented very well here at all. If you wanted to parse, say, an open office manifest file or a piece of RDF/XML, you have to get elements and attributes by the namespace url. Example: <?php function display($in) { if (file_exists($in)) { $xml = simplexml_load_file($in); } else { throw new Exception($in . " does not exist"); } $manifest = $xml->children('http://openoffice.org/2001/manifest'); foreach ($manifest->xpath('//manifest:file-entry') as $file) { foreach ($file->attributes('http://openoffice.org/2001/manifest') as $key => $value) { print "Key:" . $key . "\n"; print "Value:" . $value . "\n"; } } } ?> File: manifest.xml <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE manifest:manifest PUBLIC "-//OpenOffice.org//DTD Manifest 1.0//EN" "Manifest.dtd"> <manifest:manifest xmlns:manifest="http://openoffice.org/2001/manifest"> <manifest:file-entry manifest:media-type="application/vnd.sun.xml.writer" manifest:full-path="/"/> <manifest:file-entry manifest:media-type="application/vnd.sun.xml.ui.configuration" manifest:full-path="Configurations2/"/> <manifest:file-entry manifest:media-type="application/binary" manifest:full-path="layout-cache"/> <manifest:file-entry manifest:media-type="text/xml" manifest:full-path="content.xml"/> <manifest:file-entry manifest:media-type="text/xml" manifest:full-path="styles.xml"/> <manifest:file-entry manifest:media-type="text/xml" manifest:full-path="meta.xml"/> <manifest:file-entry manifest:media-type="" manifest:full-path="Thumbnails/thumbnail.png"/> <manifest:file-entry manifest:media-type="" manifest:full-path="Thumbnails/"/> <manifest:file-entry manifest:media-type="text/xml" manifest:full-path="settings.xml"/> </manifest:manifest> ---- Server IP: 203.220.0.234 Probable Submitter: 203.63.126.9 ---- X-Spam-Status: No, hits=3.8 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER, HTML_50_60 autolearn=no version=2.64 ---- Manual Page -- http://www.php.net/manual/en/ref.simplexml.php Edit -- http://master.php.net/note/edit/66259 Del: integrated -- http://master.php.net/note/delete/66259/integrated Del: useless -- http://master.php.net/note/delete/66259/useless Del: bad code -- http://master.php.net/note/delete/66259/bad+code Del: spam -- http://master.php.net/note/delete/66259/spam Del: non-english -- http://master.php.net/note/delete/66259/non-english Del: in docs -- http://master.php.net/note/delete/66259/in+docs Del: other reasons-- http://master.php.net/note/delete/66259 Reject -- http://master.php.net/note/reject/66259 Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#111536) next »