Avatar billede thorus Nybegynder
25. marts 2004 - 12:39

Parse XML med PHP

jeg har følgende:

Det jeg vil parse ligger her:
http://news.php.net/group.php?group=php.announce&format=rss

Der kommer noget men ikke den sidste echo tabel

*************FEJL*******
$counting=count($xmlC->obj_data->CHANNEL[0]->ITEM); //to find out the number of elements in the array
$IPADDR="";
$OWNER="";
$PUB="";
$DESC="";

for($i=0;$i<$counting;++$i)
  {
      $IPADDR[$i]=$xmlC->obj_data->CHANNEL[0]->ITEM[$i]->LINK[0];
      $OWNER[$i]=$xmlC->obj_data->CHANNEL[0]->ITEM[$i]->TITLE[0];
      $DESC[$i]=$xmlC->obj_data->CHANNEL[0]->ITEM[$i]->DESCRIPTION[0];
      $PUB[$i]=$xmlC->obj_data->CHANNEL[0]->ITEM[$i]->PUBDATE[0];
      }
  echo("<center><TABLE border=1>");
  for($i=0;$i<$counting;++$i)
    { echo "<tr><td>$IPADDR[$i]</td><td>$OWNER[$i]  $DESC[$i] $PUB[$i]</td></tr>"; }
  echo("</TABLE></center>");
*************FEJL*******

<?php
// parsing the filename.xml file for IPAddress and it's owner
//  $filename = "http://news.php.net/group.php?group=php.announce&format=rss";
  $filename = "xmltest.xml";
  $xmlC = new XmlC();
  $xml_data = file_get_contents( $filename );
 
  $xmlC->Set_XML_data( $xml_data );
 
echo( "<pre>\n" ); // shows output in browser
print_r( $xmlC->obj_data );
  echo( "</pre>\n" );
 
  class XmlC
  {
    var $xml_data;
    var $obj_data;
    var $pointer;
 
    function XmlC()
    {
    }
 
    function Set_xml_data( &$xml_data )
    {
      $this->index = 0;
      $this->pointer[] = &$this->obj_data;
 
      $this->xml_data = $xml_data;
      $this->xml_parser = xml_parser_create( "UTF-8" );
 
      xml_parser_set_option( $this->xml_parser, XML_OPTION_CASE_FOLDING, false );
      xml_set_object( $this->xml_parser, &$this );
      xml_set_element_handler( $this->xml_parser, "_startElement", "_endElement");
      xml_set_character_data_handler( $this->xml_parser, "_cData" );
 
      xml_parse( $this->xml_parser, $this->xml_data, true );
      xml_parser_free( $this->xml_parser );
    }
 
    function _startElement( $parser, $tag, $attributeList )
    {
      foreach( $attributeList as $name => $value )
      {
        $value = $this->_cleanString( $value );
        $object->$name = $value;
      }
      eval( "\$this->pointer[\$this->index]->" . $tag . "[] = \$object;" );
      eval( "\$size = sizeof( \$this->pointer[\$this->index]->" . $tag . " );" );
      eval( "\$this->pointer[] = &\$this->pointer[\$this->index]->" . $tag . "[\$size-1];" );
     
      $this->index++;
    }
 
    function _endElement( $parser, $tag )
    {
      array_pop( $this->pointer );
      $this->index--;
    }
 
    function _cData( $parser, $data )
    {
      if( trim( $data ) )
      {
        $this->pointer[$this->index] = trim( $data );
      }
    }
 
    function _cleanString( $string )
    {
      return utf8_decode( trim( $string ) );
    }
 
  }
//retriving information stored in Address and Owner arrays

$counting=count($xmlC->obj_data->CHANNEL[0]->ITEM); //to find out the number of elements in the array
$IPADDR="";
$OWNER="";
$PUB="";
$DESC="";

for($i=0;$i<$counting;++$i)
  {
      $IPADDR[$i]=$xmlC->obj_data->CHANNEL[0]->ITEM[$i]->LINK[0];
      $OWNER[$i]=$xmlC->obj_data->CHANNEL[0]->ITEM[$i]->TITLE[0];
      $DESC[$i]=$xmlC->obj_data->CHANNEL[0]->ITEM[$i]->DESCRIPTION[0];
      $PUB[$i]=$xmlC->obj_data->CHANNEL[0]->ITEM[$i]->PUBDATE[0];
      }
  echo("<center><TABLE border=1>");
  for($i=0;$i<$counting;++$i)
    { echo "<tr><td>$IPADDR[$i]</td><td>$OWNER[$i]  $DESC[$i] $PUB[$i]</td></tr>"; }
  echo("</TABLE></center>");
 

?>
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Vi tilbyder markedets bedste kurser inden for webudvikling

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester