[Help] XML Parsing with PHP

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    [Help] XML Parsing with PHP

    Hey again me with a problem lol,

    i need to get an xml file with curl and parse it ... okay so far it all works my problem now is i have no idea on how to parse it properly atm it looks like this

    Code:
    
    Array
    (
        [0] => Array
            (
                [name] => user
                [pass] => test
                [priority] => 4
            )
    
        [1] => Array
            (
    
                [name] => user
                [pass] => test
                [priority] => 4
    
            )
    
        [2] => Array
            (
        
    
                [name] => user
                [pass] => test
                [priority] => 4
            )
    
    )
    Now I dont know how to parse it so i can use the strings seperatly
    like
    PHP Code:
    echo "$user"
    etc
    ------
    why do i need to use xml and curl? sc_trans 2 has an api which outputs results as xml funny thing is it needs to be a POST so i am using curl and theres no other way around it.

    ---Edit---
    PHP Code:
              $ch curl_init();
    curl_setopt($chCURLOPT_URL'http://user:pass@domain.com:5555/api');
    curl_setopt ($chCURLOPT_POST1);
    curl_setopt ($chCURLOPT_POSTFIELDS'op=getstatus&seq=45');
    curl_setopt ($chCURLOPT_COOKIEJAR'cookie.txt');
    curl_setopt ($chCURLOPT_RETURNTRANSFER1);
    $xml curl_exec($ch);
    function 
    ExtractString($str$start$end)
    {
    $str_low strtolower($str);
    $pos_start strpos($str_low$start);
    $pos_end strpos($str_low$end, ($pos_start strlen($start)));
    if ( (
    $pos_start !== false) && ($pos_end !== false) )
    {
    $pos1 $pos_start strlen($start);
    $pos2 $pos_end $pos1;
    return 
    substr($str$pos1$pos2);
    }
    }
    $match ExtractString($xml'<name>''</name>'); 
    Thats what i use at the moment to extract the xml and its working with one part of the XML but not multiple like in the array above
    Last edited by djdevil89; 06.10.11, 00:40.

    PHP Code:
    foreach ($_SERVER as $server => $value)
    {
    echo 
    "$server is $value<br />";


    #2
    Basics of Bing Search API using .NET - CodeProject

    Comment


      #3
      thanks for the reply but im looking for a PHP solution not .NET

      PHP Code:
      foreach ($_SERVER as $server => $value)
      {
      echo 
      "$server is $value<br />";

      Comment


        #4
        try this examples:
        PHP: xml_parse - Manual
        Advertise your mobile site for FREE with AdTwirl

        Comment


          #5
          i used the link above now my result is
          Code:
          Array
          (
              [0] => Array
                  (
                      [name] => RESPONSE
                      [attrs] => Array
                          (
                              [SEQ] => 45
                          )
          
                      [children] => Array
                          (
                              [0] => Array
                                  (
                                      [name] => DATA
                                      [attrs] => Array
                                          (
                                          )
          
                                      [children] => Array
                                          (
                                              [0] => Array
                                                  (
                                                      [name] => DJLIST
                                                      [attrs] => Array
                                                          (
                                                          )
          
                                                      [children] => Array
                                                          (
                                                              [0] => Array
                                                                  (
                                                                      [name] => DJ
                                                                      [attrs] => Array
                                                                          (
                                                                          )
          
                                                                      [children] => Array
                                                                          (
                                                                              [0] => Array
                                                                                  (
                                                                                      [name] => NAME
                                                                                      [attrs] => Array
                                                                                          (
                                                                                          )
          
                                                                                      [tagData] => matrix
                                                                                  )
          
                                                                              [1] => Array
                                                                                  (
                                                                                      [name] => PASSWORD
                                                                                      [attrs] => Array
                                                                                          (
                                                                                          )
          
                                                                                      [tagData] => xxxxxx
                                                                                  )
          
                                                                              [2] => Array
                                                                                  (
                                                                                      [name] => PRIORITY
                                                                                      [attrs] => Array
                                                                                          (
                                                                                          )
          
                                                                                      [tagData] => 4
                                                                                  )
          
                                                                              [3] => Array
                                                                                  (
                                                                                      [name] => ENABLED
                                                                                      [attrs] => Array
                                                                                          (
                                                                                          )
          
                                                                                      [tagData] => 1
                                                                                  )
          
                                                                          )
          
                                                                  )
          
                                                          )
          
                                                  )
          
                                          )
          
                                  )
          
                          )
          
                  )
          
          )
          the parts i am interested in are
          Code:
          [tagData] => matrix
          and
          Code:
          [tagData] => xxxxx
          i cant get it to to list properly heres my code (yes i know it says print_r thats because right now its the only way to give me any data at all)
          PHP Code:
          <?php
          $ch 
          curl_init();
          curl_setopt($chCURLOPT_URL'http://xxxx:xxxx@domain.com:5555/api');
          curl_setopt ($chCURLOPT_POST1);
          curl_setopt ($chCURLOPT_POSTFIELDS'op=listdjs&seq=45');
          curl_setopt ($chCURLOPT_COOKIEJAR'cookie.txt');
          curl_setopt ($chCURLOPT_RETURNTRANSFER1);
          $xml curl_exec($ch);
          class 
          xml2Array {
               
               var 
          $arrOutput = array();
               var 
          $resParser;
               var 
          $strXmlData;
               
               function 
          parse($strInputXML) {
               
                       
          $this->resParser xml_parser_create ();
                       
          xml_set_object($this->resParser,$this);
                       
          xml_set_element_handler($this->resParser"tagOpen""tagClosed");
                       
                       
          xml_set_character_data_handler($this->resParser"tagData");
                   
                       
          $this->strXmlData xml_parse($this->resParser,$strInputXML );
                       if(!
          $this->strXmlData) {
                          die(
          sprintf("XML error: %s at line %d",
                       
          xml_error_string(xml_get_error_code($this->resParser)),
                       
          xml_get_current_line_number($this->resParser)));
                       }
                                       
                       
          xml_parser_free($this->resParser);
                       
                       return 
          $this->arrOutput;
               }
               function 
          tagOpen($parser$name$attrs) {
                  
          $tag=array("name"=>$name,"attrs"=>$attrs); 
                  
          array_push($this->arrOutput,$tag);
               }
               
               function 
          tagData($parser$tagData) {
                  if(
          trim($tagData)) {
                       if(isset(
          $this->arrOutput[count($this->arrOutput)-1]['tagData'])) {
                           
          $this->arrOutput[count($this->arrOutput)-1]['tagData'] .= $tagData;
                       } 
                       else {
                           
          $this->arrOutput[count($this->arrOutput)-1]['tagData'] = $tagData;
                       }
                  }
               }
               
               function 
          tagClosed($parser$name) {
                  
          $this->arrOutput[count($this->arrOutput)-2]['children'][] = $this->arrOutput[count($this->arrOutput)-1];
                  
          array_pop($this->arrOutput);
               }
           }
            
          $objXML = new xml2Array();
            
          $arrOutput $objXML->parse($xml);
           
          ?>
           <pre><?
            print_r($arrOutput); //print it out, or do whatever!

           ?>
          Last edited by djdevil89; 07.10.11, 00:48. Reason: forgot to close the PHP tag

          PHP Code:
          foreach ($_SERVER as $server => $value)
          {
          echo 
          "$server is $value<br />";

          Comment


            #6
            why u cant use SimplXML

            $xml = simplexml_load_file("test.xml");

            echo $xml->getName() . "<br />";

            foreach($xml->children() as $child)
            {
            echo $child->getName() . ": " . $child . "<br />";
            }

            Originally posted by GumSlone View Post
            try this examples:
            PHP: xml_parse - Manual
            will you please check and reply to my pm

            Comment


              #7
              simple xml aint working now when i do a echo $xml; it lists me all required data but since i need to put some entry before it i need to
              PHP Code:
              $name ExtractString($xmlres'<name>''</name>'); 
              $password ExtractString($xmlres'<password>''</password>'); 
              this is my code now
              PHP Code:
              <?php
              $ch 
              curl_init();
              curl_setopt($chCURLOPT_URL'http://xxxx:xxxx@domain.com:5555/api');
              curl_setopt ($chCURLOPT_POST1);
              curl_setopt ($chCURLOPT_POSTFIELDS'op=listdjs&seq=45');
              curl_setopt ($chCURLOPT_COOKIEJAR'cookie.txt');
              curl_setopt ($chCURLOPT_RETURNTRANSFER1);
              $xmlres curl_exec($ch);
              ?>
              <pre>
              <?
              function ExtractString($str, $start, $end)
              {
              $str_low = strtolower($str);
              $pos_start = strpos($str_low, $start);
              $pos_end = strpos($str_low, $end, ($pos_start + strlen($start)));
              if ( ($pos_start !== false) && ($pos_end !== false) )
              {                                                                                                     
              $pos1 = $pos_start + strlen($start);                                                                   
              $pos2 = $pos_end - $pos1;
              return substr($str, $pos1, $pos2);
              }
              }
              $name = ExtractString($xmlres, '<name>', '</name>'); 
              $password = ExtractString($xmlres, '<password>', '</password>'); 
              ?>
              <table style="border-collapse:collapse;width:100%;">
                <tbody>
                  <tr>
                    <td style="border:#000000 1px solid">DJ Name<br />
                      </td>
                    <td style="border:#000000 1px solid">DJ Pass<br />
                      </td>
                  </tr>
                </tbody>
              </table>
              <table style="border-collapse:collapse;width:100%;">
                <tbody>
                  <tr>
                    <td style="border:#000000 1px solid"><?
                    print_r($name);?><br />
                      </td>
              <td style="border:#000000 1px solid"><?
                    print_r($password);?><br />
                      </td>
                  </tr>
                </tbody>
              </table>
              <?
              But it only gives me one result while i know there are 2 -.-

              PHP Code:
              foreach ($_SERVER as $server => $value)
              {
              echo 
              "$server is $value<br />";

              Comment


                #8
                Lets say your array is stored in a variable called $array

                Now just do:

                foreach($array as $a) {
                echo 'name: '.$a['name'].'<br/>';
                echo 'pass: '.$a['pass'].'<br/>';
                echo 'priority: '.$a['priority'].'<br/>';
                }
                mysterio.al - programming is a functional art

                Comment


                  #9
                  Thanks mysterio but that aint working either bro, you got the script maybe youll find a solution i keep trying and trying but no result -.-

                  PHP Code:
                  foreach ($_SERVER as $server => $value)
                  {
                  echo 
                  "$server is $value<br />";

                  Comment


                    #10
                    So u only want the content between <name></name> and <password></password>?
                    Have u take into consideration to preg_match them and take matched values?
                    Something like:
                    PHP Code:
                    function get_it($from) {
                    $match preg_match_all('/(<name|password>)(\w.*)(<.*>)/ismU'$from$matched);
                    return 
                    $matched[2]; // return $matched[1] or $matched[3] for tags

                    Just separate tag in the pattern if necessary i just wrote both of for example in one.
                    Last edited by arnage; 09.10.11, 14:07.
                    <!DOCTYPE html PUBLIC "-//WAPFORUM.RS

                    Comment

                    Working...
                    X