Search code examples
phpxmlmediawiki-api

how to get attributes using PHP SimpleXML (in mediawiki-api-xml output)


im trying to get a List of Mediawiki-Articles into my php-script. I am using MediaWiki's api.php with xml output. My dummy-data look like this:

<api>
   <query-continue>
       <categorymembers cmcontinue="page|474c495353494552454e|162"/>
   </query-continue>
   <query>
       <categorymembers>
           <cm pageid="297" ns="0" title="Add move"/>
           <cm pageid="116" ns="0" title="Ascanio-Spread"/>
           <cm pageid="193" ns="0" title="Doublieren"/>
           <cm pageid="358" ns="0" title="Down-under"/>
           <cm pageid="274" ns="0" title="Einwegmuster"/>
           <cm pageid="227" ns="0" title="Elmsley-Count"/>
           <cm pageid="130" ns="0" title="Falsch abheben"/>
           <cm pageid="144" ns="0" title="Falsch mischen"/>
           <cm pageid="565" ns="0" title="Filieren"/>
           <cm pageid="148" ns="0" title="Forcieren"/>
      </categorymembers>
   </query>
</api>

I am looking for all the "titles" in the cm-Containers.

My Script looks like this:

 $xml = simplexml_load_file($mywiki);
 foreach($xml->cm[0]->attributes() as $a => $b)
 {
   echo $a,'="',$b,"<br>";
 }

Which is intended to give my all attributes of the first cm-container. But that wont work, I get error:

Call to a member function attributes() on null

Solution

  • First of all: Before posting a question, please be explicit about your own debugging efforts. You should have checked this:

    var_dump( $xml->cm[0] );
    

    Next, you might wish to do something like this:

    $xml->query[0]->categorymembers[0]->cm[0]
    

    Go along the path down to the node in question.