Hi friends,In this post i would like to explain about curd operations ( add, edit and delete ) data using xml file. It is simple to maintain and rapid in the operations, it will be useful when simple operations required.
Below code explain the how to insert , edit , view and delete operations using database and xml format.

xml crud operations using php | Anil Labs

xml crud operations using php | Anil Labs

Connection establish :

Mysql :

1
2
   $con = mysql_connect('Server Host Name','User Name' ,'Password');
   mysql_select_db('Database name',$con);

XML :

1
2
   $xml = simplexml_load_file("users.xml"); //This line will load the XML file.
   $sxe = new SimpleXMLElement($xml->asXML());

View the details:

Mysql:

1
2
3
4
5
    <?php $query   = "SELECT * FROM users";
          $result  = mysql_query($query);
          while($resUsers = mysql_fetch_assoc($result)){
            echo $resUsers['name']
          } ?>

XML :

1
2
3
4
     $rows = count($sxe);
     for($i=0;$i<$no_rows;$i++){
       echo  ($sxe->users[$i]->name);
       }

Insert the details:

Mysql:

1
2
    $query = "insert into users(id,name)values($id,'$name')";
    $result = mysql_query($quesry);

XML:

1
2
3
4
5
    $no_rows = count($sxe);
    $query = $sxe->addChild("users");  
    $query->addChild("id", $no_rows+1);
    $query->addChild("name",  ($name));
    $sxe->asXML("users.xml");

Edit the details:

Mysql :

1
2
3
4
    $query = "select * from users where id=".$edit_id;
    //and update query :
    $query_update = "update users set name = '$name' where id=$edit_id";
    $result = mysql_query($query_update);

XML:

1
2
3
4
5
    <?php $rows = count($sxe);
    for($i = 0, $length = $rows; $i < $length; $i++){
       if($sxe->users[$i]->id == $edit_id){ ?>
       <input value="<?php echo $sxe->users[$i]->name;?>">
       <? }

Update:

1
2
3
4
5
6
7
    $rows = count($sxe);
    for($i = 0, $length = $rows; $i < $length; $i++){
         if($sxe->users[$i]->id == $edit_id){
            $sxe->users[$i]->name =  ($name);
          }
    }
    $sxe->asXML("users.xml");

Delete the details:

Mysql :

1
2
    $query = "delete from users where id=".$edit_id;
    $result = mysql_query($query);

XML :

1
2
3
4
5
6
7
8
    $rows = count($sxe);
    for($i = 0, $length = $rows; $i < $length; $i++){
        if($sxe->users[$i]->id == $edit_id){
            unset($sxe->users[$i]);
            break;
        }
        }
    $sxe->asXML("users.xml");

Database:

1
2
3
4
CREATE TABLE `users` (
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`name` VARCHAR( 250 ) NOT NULL
)

XML File:

users.xml

1
2
3
4
5
6
7
<?xml version="1.0" encoding="iso-8859-1"?>
<userdetails>
    <users>
        <id></id>
        <name></name>
    </users>
</users>

5 Comments

Jagjit Singh · February 22, 2011 at 6:44 am

it is good but not working online .suggest some soultion

olind sihombing · September 23, 2011 at 8:55 am

Nice post anil 🙂

ritesh · February 21, 2013 at 2:00 pm

I have problem in my xml size 600 mb or large same type of 20 other files
i want to insert into mysql table
xml format like

147OmahaSteaks.com, Inc.2013-02-15/11:08:17Food & DrinkSeafood Appetizershttp://click.linksynergy.com/link?id=q6gkJqWDpGA&offerid=7537.2182&type=15&murl=http%3A%2F%2Fwww.omahasteaks.com%2Fservlet%2Foh%3FDSP%3D14%26AID%3D1%26SRC%3DAE6055%26ITMSUF%3DHHW%26IID%3D2182%26partnerid%3Dcpcs.linksharehttp://www.omahasteaks.com/gifs/big/sf098.jpghttp://click.linksynergy.com/link?id=q6gkJqWDpGA&offerid=7537.2182&type=15&murl=http%3A%2F%2Fwww.omahasteaks.com%2Fservlet%2Foh%3FDSP%3D14%26AID%3D1%26SRC%3DAE2886%26ITMSUF%3DNSF%26IID%3D21821 (1.5 lb. pkg.) Oven Fried ShrimpA traditional favorite at seafood restaurants, our Oven Fried Shrimp is sure to become a family favorite, too! We start with tender, meaty jumbo shrimp then we dip them in a golden crispy batter. All you do is bake and serve! It doesn’t get much easier than that. Pair them with our Sweet and Tangy Cocktail sauce for the perfect appetizer, or serve with fries and coleslaw for a delicious meal.8.00amount17.9925.99Omaha Steaks13.99UPS, GROUNDshrimp appetizer, fried shrimp, jumbo shrimp, tempura shrimp, baked shrimp, seafood, seafood appetizer, appetizers, easy appetizers, party appetizershttp://ad.linksynergy.com/fs-bin/show?id=q6gkJqWDpGA&bids=7537.2182&type=15&subid=0
Food & Drink4 – 8 Steak Combinationshttp://click.linksynergy.com/link?id=q6gkJqWDpGA&offerid=7537.501&type=15&murl=http%3A%2F%2Fwww.omahasteaks.com%2Fservlet%2Foh%3FDSP%3D14%26AID%3D1%26SRC%3DAE6055%26ITMSUF%3DHHW%26IID%3D501%26partnerid%3Dcpcs.linksharehttp://www.omahasteaks.com/gifs/big/cmb501.jpghttp://click.linksynergy.com/link?id=q6gkJqWDpGA&offerid=7537.501&type=15&murl=http%3A%2F%2Fwww.omahasteaks.com%2Fservlet%2Foh%3FDSP%3D14%26AID%3D1%26SRC%3DAE2886%26ITMSUF%3DNSF%26IID%3D501The EmperorOur most tender steak, the Filet Mignon, together with our most robust steak, the Top Sirloin … it’s a combination fit for royalty! You’ll love our tender, mild Filet Mignons … for a truly elegant steak experience. Looking for bold, beefy flavor? Look no further than our lean, flavorful Top Sirloins.85.01amount89.99175.00Omaha Steaks19.99UPS, GROUNDSteak combos, steak assortments, steak variety packshttp://ad.linksynergy.com/fs-bin/show?id=q6gkJqWDpGA&bids=7537.501&type=15&subid=0
Food & Drink24 oz. Porterhouse Steakshttp://click.linksynergy.com/link?id=q6gkJqWDpGA&offerid=7537.1640&type=15&murl=http%3A%2F%2Fwww.omahasteaks.com%2Fservlet%2Foh%3FDSP%3D14%26AID%3D1%26SRC%3DAE6055%26ITMSUF%3DHHW%26IID%3D1640%26partnerid%3Dcpcs.linksharehttp://www.omahasteaks.com/gifs/big/tb011.jpghttp://click.linksynergy.com/link?id=q6gkJqWDpGA&offerid=7537.1640&type=15&murl=http%3A%2F%2Fwww.omahasteaks.com%2Fservlet%2Foh%3FDSP%3D14%26AID%3D1%26SRC%3DAE2886%26ITMSUF%3DNSF%26IID%3D16402 (24 oz.) Porterhouse SteaksNothing satisfies a really big steak appetite like an Omaha Steaks Porterhouse! It’s like having a whole Filet Mignon and a whole Strip Sirloin on your plate…all with the flavor-enhancing bone left in!50.01amount79.99130.00Omaha Steaks16.99UPS, GROUNDPorterhouse Steak, beef tenderloin, strip steak, tbonehttp://ad.linksynergy.com/fs-bin/show?id=q6gkJqWDpGA&bids=7537.1640&type=15&subid=0
Food & DrinkVegetableshttp://click.linksynergy.com/link?id=q6gkJqWDpGA&offerid=7537.1518&type=15&murl=http%3A%2F%2Fwww.omahasteaks.com%2Fservlet%2Foh%3FDSP%3D14%26AID%3D1%26SRC%3DAE6055%26ITMSUF%3DHHW%26IID%3D1518%26partnerid%3Dcpcs.linksharehttp://www.omahasteaks.com/gifs/big/sp041.jpghttp://click.linksynergy.com/link?id=q6gkJqWDpGA&offerid=7537.1518&type=15&murl=http%3A%2F%2Fwww.omahasteaks.com%2Fservlet%2Foh%3FDSP%3D14%26AID%3D1%26SRC%3DAE2886%26ITMSUF%3DNSF%26IID%3D15182 (7 oz. trays) Roasted Vegetable MedleyA medley of fire roasted vegetables including carrots, cauliflower, sugar snap peas and pepper strips in a seasoned butter sauce are the perfect complement to any meal!11.00amount9.9920.99Omaha Steaks13.99UPS, GROUNDSide dishes, stuffed baked potatoes, vegetables, risotto cakes, Spanish rice, gourmet recipeshttp://ad.linksynergy.com/fs-bin/show?id=q6gkJqWDpGA&bids=7537.1518&type=15&subid=0
571

one xml min 200000 or 500000 record please guide me

Amitabha Roy · May 15, 2015 at 6:37 am

I was looking for XML CRUD examples in PHP and your code works fine at my end. Thanks for sharing.

Tweets that mention xml crud operations using php « ANIL KUMAR PANIGRAHI 's Blog -- Topsy.com · January 21, 2011 at 9:14 am

[…] This post was mentioned on Twitter by Arun Bala Ganesan, Anil Kumar Panigrahi. Anil Kumar Panigrahi said: xml crud operations using php : http://www.anil2u.info/2010/12/18/xml-crud-operations-using-php/ […]

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *