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
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!

Link to comment
Share on other sites

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'.

 

Link to comment
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!

 

Cool.

Link to comment
Share on other sites

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');
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.