• 0

[XML] Help with XML


Question

Hi there!

I've got a question. My XML document has 3 kind of category's, and my script that is parsing the XML collects always the top one. But that is not what I want, I want to collect the "/live" mount category. But when the "/live" category isn't availible, I want to fetch the "/autodj" category. How can I get this together?

<source mount="/autodj">

<source mount="/live">

<source mount="/stream">


   function openstats() {
      $fp = @fopen("http://admin:".$this-&gt;passwd."@".$this-&gt;host.":".$this-&gt;port."/admin/stats", "r");
      if (!$fp) {
         $this-&gt;_error = "$errstr ($errno)";
         return(0);
      } else {
          while (!feof($fp)) {
               $this-&gt;_xml .= fgets($fp, 512);
          }
          fclose($fp);

         if(!isset($this-&gt;_xml)) {
            $this-&gt;_error = "Bad login";
            return(0);
         }

         $xmlparser = xml_parser_create();
         if (!xml_parse_into_struct($xmlparser, $this-&gt;_xml, $this-&gt;_values, $this-&gt;_indexes)) {
            $this-&gt;_error = "Unparsable XML";
            return(0);
         }

         xml_parser_free($xmlparser);

         return(1);
      }
   }

Link to comment
Share on other sites

1 answer to this question

Recommended Posts

  • 0

Presumably the various source nodes don't exist in the XML if they're not available?

Use SimpleXML and it's xpath functionality. It makes searching XML much easier. If you visualise XML as a tree structure it makes heaps more sense.

This is just a basic example, which is totally untested but it should give you something to work with.

$xml_object = simplexml_load_string($this-&gt;_xml);

// Check for a source node with an mount
if ($cat_live = $xml_object-&gt;xpath('/source[@mount="/live"]')) {

 // Use the live category

} else if ($cat_autodj = $xml_object-&gt;xpath('/source[@mount="/autodj"]')) {

 // Use the autodj category.

}

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.