Loading ...
Sorry, an error occurred while loading the content.
 

Re: [phpResource] Class for XML Parsing

Expand Messages
  • Rupom
    Sure we can solve many problems by ereg, preg, ereg_replace, preg_replace etc. And one thing I want to share with you - we should not guess any format (as u
    Message 1 of 8 , Mar 6, 2006
      Sure we can solve many problems by ereg, preg, ereg_replace,
      preg_replace etc. And one thing I want to share with you - we should
      not guess any format (as u guess a lot) . We all have to write code in
      such a way so that all formatting are well-formatted and usable to our
      code. By this way, a generalized version can be made.



      piff lord <lord_piff@...> wrote:
      hi,
      sorry for the late replay, i was away for 3 days, as for the script probem is u haven't solved the problem yet u could very easily use ereg and ereg replace if all the code is on one single line, but honestly i have never seen a xml file on one single page.. try reading the ereg and ereg_replace man at www.php.net and if u still have any problems pls send me an email explaining ur exact problem, i'll be glad to help

      Rupom <rupom_315@...> wrote:
      Hi,
      Your approach is good. But when we need a generalized
      version of such parsing, we have to think a lot about
      the file structure. May be the data always not in same
      format. So your approach fails to meet that demand. Lets
      say a single line consists of more than one node, then what will
      be the situation?

      But I think your approach is very good and light-weight in context
      of structured xml data.

      Thanks...

      Rupom

      piff lord <lord_piff@...> wrote:
      hi, i'm sorry i recently joined the group and i haven't read the initial email, but if u want to create an array from a xml file i used "file" function, and it's very usefull, it creates an array, and every element of the array is a row from the file ...
      here's an example of the usage

      $xml=file("xml file");
      while($xml)
        trim(strip_tags($xml));
       
      and that's about all there is to it ...
      hope this is of any help to u
      Rupom <rupom_315@...> wrote:
      Shimul,
      What about the following class? It creates a hierarchical array from XML data.

      ------------------------------------class.xmltoarray.php-----------------------------------------------------------------------------
      <?php

      class XmlToArray
      {
         
          var $xml='';
         
          /**
          * Default Constructor
          * @param $xml = xml data
          * @return none
          */
         
          function XmlToArray($xml)
          {
             $this->xml = $xml;   
          }
         
          /**
          * _struct_to_array($values, &$i)
          *
          * This is adds the contents of the return xml into the array for easier processing.
          * Recursive, Static
          *
          * @access    private
          * @param    array  $values this is the xml data in an array
          * @param    int    $i  this is the current location in the array
          * @return    Array
          */
         
          function _struct_to_array($values, &$i)
          {
              $child = array();
              if (isset($values[$i]['value'])) array_push($child, $values[$i]['value']);
             
              while ($i++ < count($values)) {
                  switch ($values[$i]['type']) {
                      case 'cdata':
                      array_push($child, $values[$i]['value']);
                      break;
                     
                      case 'complete':
                          $name = $values[$i]['tag'];
                          if(!empty($name)){
                          $child[$name]= ($values[$i]['value'])?($values[$i]['value']):'nothing->Razzaque';
                          if(isset($values[$i]['attributes'])) {                   
                              $child[$name] = $values[$i]['attributes'];
                          }
                      }   
                    break;
                     
                      case 'open':
                          $name = $values[$i]['tag'];
                          $size = isset($child[$name]) ? sizeof($child[$name]) : 0;
                          $child[$name][$size] = $this->_struct_to_array($values, $i);
                      break;
                     
                      case 'close':
                      return $child;
                      break;
                  }
              }
              return $child;
          }//_struct_to_array
         
          /**
          * createArray($data)
          *
          * This is adds the contents of the return xml into the array for easier processing.
          *
          * @access    public
          * @param    string    $data this is the string of the xml data
          * @return    Array
          */
          function createArray()
          {
              $xml    = $this->xml;
              $values = array();
              $index  = array();
              $array  = array();
              $parser = xml_parser_create();
              xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
              xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
              xml_parse_into_struct($parser, $xml, $values, $index);
              xml_parser_free($parser);
              $i = 0;
              $name = $values[$i]['tag'];
              $array[$name] = isset($values[$i]['attributes']) ? $values[$i]['attributes'] : '';
              $array[$name] = $this->_struct_to_array($values, $i);
              return $array;
          }//createArray
         
         
      }//XmlToArray


      ?>

      --------------------------------------------------------------------------------------------------

      Example usage:

      -----------------------------------------usage.php---------------------------------------------

      <?php

      require_once "class.xmltoarray.php";

      //XML Data
      $xml_data = "
      <result>
        <username>testing</username>
        <password>foobarbaz</password>
        <name>enygma</name>
      </result>";

      //Creating Instance of the Class
      $xmlObj    = new XmlToArray($xml_data);
      //Creating Array
      $arrayData = $xmlObj->createArray();

      //Displaying the Array
      echo "<pre>";
      print_r($arrayData);
      echo "</pre>";

      ?>

      ---------------------------------------------------------------------------------------------------

      Regards,
      Rupom



       
      Join "phpResource" Group And Enjoy PHPing...

      Yahoo! Mail
      Bring photos to life! New PhotoMail makes sharing a breeze.


      Yahoo! Mail
      Bring photos to life! New PhotoMail makes sharing a breeze.



       
      Join "phpResource" Group And Enjoy PHPing...

      Yahoo! Mail
      Bring photos to life! New PhotoMail makes sharing a breeze.


      Yahoo! Mail
      Use Photomail to share photos without annoying attachments.


      Relax. Yahoo! Mail virus scanning helps detect nasty viruses!

    • piff lord
      man, sry about this but i have not the slightest ideea about what u are talking about, i mean u wanted to parse a xml file, well my initial code worked
      Message 2 of 8 , Mar 6, 2006
        man,
        sry about this but i have not the slightest ideea about what u are talking about, i mean u wanted to parse a xml file, well my initial code worked perfectly every time i used it, but that code was only to create an array form a xml file, and take out the tags, the search for the element wanted is made with another code, but that depends on what u are looking for ....

        Rupom <rupom_315@...> wrote:


        Sure we can solve many problems by ereg, preg, ereg_replace,
        preg_replace etc. And one thing I want to share with you - we should
        not guess any format (as u guess a lot) . We all have to write code in
        such a way so that all formatting are well-formatted and usable to our
        code. By this way, a generalized version can be made.



        piff lord <lord_piff@...> wrote:
        hi,
        sorry for the late replay, i was away for 3 days, as for the script probem is u haven't solved the problem yet u could very easily use ereg and ereg replace if all the code is on one single line, but honestly i have never seen a xml file on one single page.. try reading the ereg and ereg_replace man at www.php.net and if u still have any problems pls send me an email explaining ur exact problem, i'll be glad to help

        Rupom <rupom_315@...> wrote:
        Hi,
        Your approach is good. But when we need a generalized
        version of such parsing, we have to think a lot about
        the file structure. May be the data always not in same
        format. So your approach fails to meet that demand. Lets
        say a single line consists of more than one node, then what will
        be the situation?

        But I think your approach is very good and light-weight in context
        of structured xml data.

        Thanks...

        Rupom

        piff lord <lord_piff@...> wrote:
        hi, i'm sorry i recently joined the group and i haven't read the initial email, but if u want to create an array from a xml file i used "file" function, and it's very usefull, it creates an array, and every element of the array is a row from the file ...
        here's an example of the usage

        $xml=file("xml file");
        while($xml)
          trim(strip_tags($xml));
         
        and that's about all there is to it ...
        hope this is of any help to u
        Rupom <rupom_315@...> wrote:
        Shimul,
        What about the following class? It creates a hierarchical array from XML data.

        ------------------------------------class.xmltoarray.php-----------------------------------------------------------------------------
        <?php

        class XmlToArray
        {
           
            var $xml='';
           
            /**
            * Default Constructor
            * @param $xml = xml data
            * @return none
            */
           
            function XmlToArray($xml)
            {
               $this->xml = $xml;   
            }
           
            /**
            * _struct_to_array($values, &$i)
            *
            * This is adds the contents of the return xml into the array for easier processing.
            * Recursive, Static
            *
            * @access    private
            * @param    array  $values this is the xml data in an array
            * @param    int    $i  this is the current location in the array
            * @return    Array
            */
           
            function _struct_to_array($values, &$i)
            {
                $child = array();
                if (isset($values[$i]['value'])) array_push($child, $values[$i]['value']);
               
                while ($i++ < count($values)) {
                    switch ($values[$i]['type']) {
                        case 'cdata':
                        array_push($child, $values[$i]['value']);
                        break;
                       
                        case 'complete':
                            $name = $values[$i]['tag'];
                            if(!empty($name)){
                            $child[$name]= ($values[$i]['value'])?($values[$i]['value']):'nothing->Razzaque';
                            if(isset($values[$i]['attributes'])) {                   
                                $child[$name] = $values[$i]['attributes'];
                            }
                        }   
                      break;
                       
                        case 'open':
                            $name = $values[$i]['tag'];
                            $size = isset($child[$name]) ? sizeof($child[$name]) : 0;
                            $child[$name][$size] = $this->_struct_to_array($values, $i);
                        break;
                       
                        case 'close':
                        return $child;
                        break;
                    }
                }
                return $child;
            }//_struct_to_array
           
            /**
            * createArray($data)
            *
            * This is adds the contents of the return xml into the array for easier processing.
            *
            * @access    public
            * @param    string    $data this is the string of the xml data
            * @return    Array
            */
            function createArray()
            {
                $xml    = $this->xml;
                $values = array();
                $index  = array();
                $array  = array();
                $parser = xml_parser_create();
                xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
                xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
                xml_parse_into_struct($parser, $xml, $values, $index);
                xml_parser_free($parser);
                $i = 0;
                $name = $values[$i]['tag'];
                $array[$name] = isset($values[$i]['attributes']) ? $values[$i]['attributes'] : '';
                $array[$name] = $this->_struct_to_array($values, $i);
                return $array;
            }//createArray
           
           
        }//XmlToArray


        ?>

        --------------------------------------------------------------------------------------------------

        Example usage:

        -----------------------------------------usage.php---------------------------------------------

        <?php

        require_once "class.xmltoarray.php";

        //XML Data
        $xml_data = "
        <result>
          <username>testing</username>
          <password>foobarbaz</password>
          <name>enygma</name>
        </result>";

        //Creating Instance of the Class
        $xmlObj    = new XmlToArray($xml_data);
        //Creating Array
        $arrayData = $xmlObj->createArray();

        //Displaying the Array
        echo "<pre>";
        print_r($arrayData);
        echo "</pre>";

        ?>

        ---------------------------------------------------------------------------------------------------

        Regards,
        Rupom



         
        Join "phpResource" Group And Enjoy PHPing...

        Yahoo! Mail
        Bring photos to life! New PhotoMail makes sharing a breeze.


        Yahoo! Mail
        Bring photos to life! New PhotoMail makes sharing a breeze.



         
        Join "phpResource" Group And Enjoy PHPing...

        Yahoo! Mail
        Bring photos to life! New PhotoMail makes sharing a breeze.


        Yahoo! Mail
        Use Photomail to share photos without annoying attachments.


        Relax. Yahoo! Mail virus scanning helps detect nasty viruses!


        Yahoo! Mail
        Use Photomail to share photos without annoying attachments.

      • Stranger
        for mail attatchment u can use phpmailer or libmail class. here i m giving u linmail class and its sample code. if u want to attatch a file from your client
        Message 3 of 8 , Mar 6, 2006
          for mail attatchment u can use phpmailer or libmail class.  here i'm giving u linmail class and its sample code.

          if u want to attatch a file from your client machine then at first u  have to upload your file to server .

          at the page where u r sending your mail include these code instead og php mail function :

          --------------------------
          include "libmail.php";

          /*
          upload file to speacific path
          */

          $filename=$uploadPathName;


          // sending mail
          $to='shimul39@...';


          $m = new Mail(); // create the mail
          $m->From("shimul39@...");
          $m->To($to);
          $m->Subject( $subject );
          $m->Body($HTML_BLOCK);
          $m->Cc( "shimul39@...");
          $m->Priority(4);

          $m->Attach( $filename, "image/jpeg", "attachment" );

          $m->Send(); // send the mail

          echo "Mail was sent:";
          //echo $m->Get(); // show the mail source

          if u need any further instruction . pls inform me

          Stranger

          --- In phpresource@yahoogroups.com, "Sujoy Dutta" <sujoydutta1980@...> wrote:
          >
          > Hi All,
          >
          > Can you help me in a program which attach a image file with email in PHP.
          >
          > I want to send email using mail function but with a image file attachment.
          >
          > Please help me in this problem.
          >
          > Sujoy
          >


          Relax. Yahoo! Mail virus scanning helps detect nasty viruses!

        Your message has been successfully submitted and would be delivered to recipients shortly.