Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP SimpleXML parsing XML with namespaces
    text
    copied!<p>I have XML like this</p> <pre><code>&lt;atom:feed xmlns="http://kosapi.feld.cvut.cz/schema/3" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:osearch="http://a9.com/-/spec/opensearch/1.1/" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xml:base="https://kosapi.fit.cvut.cz/api/3/" xml:lang="cs"&gt; &lt;atom:entry&gt; &lt;atom:id&gt;urn:cvut:kos:parallel:353810000&lt;/atom:id&gt; &lt;atom:updated&gt;2013-06-02T14:15:54.0&lt;/atom:updated&gt; &lt;atom:author&gt; &lt;atom:name&gt;kalvotom&lt;/atom:name&gt; &lt;/atom:author&gt; &lt;atom:link rel="self" href="parallels/353810000/"/&gt; &lt;atom:content atom:type="xml" xsi:type="parallel"&gt; &lt;capacity&gt;24&lt;/capacity&gt; &lt;capacityOverfill&gt;DENIED&lt;/capacityOverfill&gt; &lt;code&gt;101&lt;/code&gt; &lt;course xlink:href="courses/BI-EFA/"&gt;Efektivní algoritmy&lt;/course&gt; &lt;enrollment&gt;DENIED&lt;/enrollment&gt; &lt;occupied&gt;0&lt;/occupied&gt; &lt;parallelType&gt;TUTORIAL&lt;/parallelType&gt; &lt;semester xlink:href="semesters/B131/"&gt;Zimní 2013/2014&lt;/semester&gt; &lt;timetableSlot&gt; &lt;day&gt;2&lt;/day&gt; &lt;duration&gt;2&lt;/duration&gt; &lt;firstHour&gt;1&lt;/firstHour&gt; &lt;parity&gt;BOTH&lt;/parity&gt; &lt;room xlink:href="rooms/T9:346/"&gt;T9:346&lt;/room&gt; &lt;/timetableSlot&gt; &lt;/atom:content&gt; &lt;/atom:entry&gt; &lt;/atom:feed&gt; </code></pre> <p>I'm writing a PHP script and need to extract contents of <code>&lt;capacity&gt;</code>, <code>&lt;occupied&gt;</code> and <code>&lt;enrollment&gt;</code> tags for every <code>&lt;atom:entry&gt;</code> (there are actually multiple <code>&lt;atom:entry&gt;</code> tags). Now, I looked up some tips about how to deal with namespaces but still wasn't able to make it work. My code</p> <pre><code>$xml = simplexml_load_file("someurl"); $xml-&gt;registerXPathNamespace('prefix', 'http://www.w3.org/2005/Atom'); foreach($xml-&gt;xpath("//prefix:entry") as $entry) { $entry-&gt;registerXPathNamespace('prefix', 'http://www.w3.org/2005/Atom'); $content = $entry-&gt;xpath("//prefix:content"); echo $content-&gt;enrollment . " | " . $content-&gt;occupied . "/" . $content-&gt;capacity . "&lt;br /&gt;\n"; } </code></pre> <p>I get</p> <blockquote> <p>Notice: Trying to get property of non-object</p> </blockquote>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload