Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to import an xml file in php?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 289
    Comment on it

    If we are required to import an xml file through core php,
    lets how can we do it:

     

    First of all create a form of enctype as "multipart/form-data" as we have created below in our index.html
     
     

    <html>
    <body>
      <form enctype="multipart/form-data" action="import.php" method="post">
      <input type="hidden" name="MAX_FILE_SIZE" value="2000000" />
      <table width="600">
        <tr>
          <td>Names file:</td>
          <td><input type="file" name="file" /></td>
          <td><input type="submit" value="Upload" /></td>
        </tr>
      </table>
    </form>
    </body>
    </html>

     

    In the above code we have created a form with action as "import.php" and method as "post".
    our form has a input field of type "file" and a submit button.

     

    Now, create a php file import.php and add below code.

     

    <?php
    $data = array();
    
    function add_pramas( $first, $middle, $last, $email )
    {
        global $data;
       
    
        $data []= array(
            'first' => $first,
            'middle' => $middle,
            'last' => $last,
            'email' => $email
            );
    }
    
    if ( $_FILES['file']['tmp_name'] )
    {
        $dom = DOMDocument::load( $_FILES['file']['tmp_name'] );
        $rows = $dom->getElementsByTagName( 'Row' );
        $first_row = true;
        foreach ($rows as $row)
        {
            if ( !$first_row )
            {
                $first = "";
                $middle = "";
                $last = "";
                $email = "";
               
                $index = 1;
                $cells = $row->getElementsByTagName( 'Cell' );
                foreach( $cells as $cell )
                {
                    $ind = $cell->getAttribute( 'Index' );
                    if ( $ind != null ) $index = $ind;
                   
                    if ( $index == 1 ) $first = $cell->nodeValue;
                    if ( $index == 2 ) $middle = $cell->nodeValue;
                    if ( $index == 3 ) $last = $cell->nodeValue;
                    if ( $index == 4 ) $email = $cell->nodeValue;
                   
                    $index += 1;
                }
                add_pramas( $first, $middle, $last, $email );
            }
            $first_row = false;
        }
    }
    ?>
    <html>
    <body>
        <table>
            <tr>
                <th>First</th>
                <th>Middle</th>
                <th>Last</th>
                <th>Email</th>
            </tr>
            <?php foreach( $data as $row ) { ?>
                <tr>
                    <td><?php echo( $row['first'] ); ?></td>
                    <td><?php echo( $row['middle'] ); ?></td>
                    <td><?php echo( $row['last'] ); ?></td>
                    <td><?php echo( $row['email'] ); ?></td>
                </tr>
                <?php } ?>
            </table>
        </body>
        </html>

     

    In the above code we can check that in the if condition we are checking for the post parameter of type file  $_FILES['file']['tmp_name'].


    If it contains the file then in the if condition we have loaded an object 'dom' of DOMDocument by passing our file in that. Now, we have set up the row parameter with the use of the dom object and the method getElementsByTagName().

     

    Now in the foreach loop we can see that in the if condition we have initialize the parameters $first, $middle, $last, $email as null and $index with 1 value, after that we have made an object of cells as '$cells'.


    Afterwards in the foreach loop we have fetched the node value of each cell with '$cell->nodeValue' and set the values of $first, $middle, $last, $email with the node values.

     

    Afterwards, we have passed these parameters value to the method add_pramas() which then set  our data array. This data array is then passed to the foreach loop of the table below and are printed.

     

    In this way we can import an xml file with core php.

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: