Jump to content

[SOLVED] xmlparse->GetXMLTree();


Crew-Portal

Recommended Posts

  $xml = $xmlparse->GetXMLTree('http://24.76.166.219:81/xml'); This is the script I am using to make a XML conection on a server that I operate. The default time for the connection is 60 seconds. As is if the server doesnt respond within 60 seconds the connection dies and I get an error message. But how do I change the Connection time allowed to 20 seconds and as the server attempts to connect I get a message like "Conecting To 24.76.166.219/xml" and once connection is sucessful it refreshes to another page with data from the XML You kinda get what I mean?

Link to comment
https://forums.phpfreaks.com/topic/81100-solved-xmlparse-getxmltree/
Share on other sites

Oh no i dont use file get contents its puts the data into an array for me. So lets say the XML looks like:

<1>

<2>

  <3>

  This Value 3

  </3>

</2>

</1>

 

The script calls it like $xml['1']['0']['2']['0']['3']['0']['value']

 

But yes the timeout thing works great thank you!

Didn't explain that very well but I'm not 100% sure that stream-set-timeout() works with file_get_contents() but I know it works with fopen().  I know that init_set() works with file_get_contents().

 

The other option you have is to use curl() and curl_setopt () with 'CURLOPT_CONNECTTIMEOUT'.

 

Oh no i dont use file get contents its puts the data into an array for me. So lets say the XML looks like:

<1>

<2>

  <3>

  This Value 3

  </3>

</2>

</1>

 

The script calls it like $xml['1']['0']['2']['0']['3']['0']['value']

 

But yes the timeout thing works great thank you!

 

Cool.

To Give you the code because you seem interested:

<?php
class ParseXML{
      function GetChildren($vals, &$i) { 
         $children = array(); // Contains node data
         if (isset($vals[$i]['value'])){
            $children['VALUE'] = $vals[$i]['value'];
         } 
         
         while (++$i < count($vals)){ 
            switch ($vals[$i]['type']){
               
            case 'cdata': 
               if (isset($children['VALUE'])){
                  $children['VALUE'] .= $vals[$i]['value'];
               } else {
                  $children['VALUE'] = $vals[$i]['value'];
               } 
            break;
            
            case 'complete':
               if (isset($vals[$i]['attributes'])) {
                  $children[$vals[$i]['tag']][]['ATTRIBUTES'] = $vals[$i]['attributes'];
                  $index = count($children[$vals[$i]['tag']])-1;
         
                  if (isset($vals[$i]['value'])){ 
                     $children[$vals[$i]['tag']][$index]['VALUE'] = $vals[$i]['value']; 
                  } else {
                     $children[$vals[$i]['tag']][$index]['VALUE'] = '';
                  }
               } else {
                  if (isset($vals[$i]['value'])){
                     $children[$vals[$i]['tag']][]['VALUE'] = $vals[$i]['value']; 
                  } else {
                     $children[$vals[$i]['tag']][]['VALUE'] = '';
                  } 
               }
            break;
            
            case 'open': 
               if (isset($vals[$i]['attributes'])) {
                  $children[$vals[$i]['tag']][]['ATTRIBUTES'] = $vals[$i]['attributes'];
                  $index = count($children[$vals[$i]['tag']])-1;
                  $children[$vals[$i]['tag']][$index] = array_merge($children[$vals[$i]['tag']][$index],$this->GetChildren($vals, $i));
               } else {
                  $children[$vals[$i]['tag']][] = $this->GetChildren($vals, $i);
               }
            break; 
         
            case 'close': 
               return $children; 
         } 
      }
   }
     
      function GetXMLTree($xmlloc){ 
         if (file_exists($xmlloc)){
            $data = implode('', file($xmlloc)); 
         } else {
            $fp = fopen($xmlloc,'r');
            while(!feof($fp)){
               $data = $data . fread($fp, 1024);
            }
      
            fclose($fp);
         }
      
         $parser = xml_parser_create('ISO-8859-1');
         xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1); 
         xml_parse_into_struct($parser, $data, $vals, $index); 
         xml_parser_free($parser); 
      
         $tree = array(); 
         $i = 0; 
      
         if (isset($vals[$i]['attributes'])) {
            $tree[$vals[$i]['tag']][]['ATTRIBUTES'] = $vals[$i]['attributes']; 
            $index = count($tree[$vals[$i]['tag']])-1;
            $tree[$vals[$i]['tag']][$index] =  array_merge($tree[$vals[$i]['tag']][$index], $this->GetChildren($vals, $i));
         } else {
            $tree[$vals[$i]['tag']][] = $this->GetChildren($vals, $i); 
         }
      return $tree; 
      }
}

$xmlparse = &new ParseXML;
   $xml = $xmlparse->GetXMLTree('http://24.76.166.219:81/xml');
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.