Results 1 to 4 of 4

Thread: How to read and write the XML DOM with PHP

  1. #1
    Join Date
    Dec 2008
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to read and write the XML DOM with PHP

    Hey,
    I am a new bee here and hope to have some replies from your end.

    I am working on reading and writing the XML DOM File using PHP.
    My XML File is In this Format

    portfolio category="Category1">
    <item thumbimg="images/thumb1.jpg" largeimg1="images/large1_1.jpg" largeimg2="images/large1_2.jpg" largeimg3="images/large1_3.jpg" title="Item 1" type="Item 1 type" detail="Item 1 description" company="Invent Solution" year="2002-03" />
    <item thumbimg="images/thumb1.jpg" largeimg1="images/large1_1.jpg" largeimg2="images/large1_2.jpg" largeimg3="images/large1_3.jpg" title="Item 2" type="Item 2 type" detail="Item 2 description" company="Invent Solution 2" year="2007-08" />
    </portfolio>

    Now i am unable to read and write data in this XML Format. Can any one help me out that how to write, read, edit and delete the data from this XML file using PHP. I will be very grateful who will help me in this regard. I am badly stuck. Kindly help me please.

    Best regards,
    Mohsin Rafique

  2. #2
    Join Date
    Jul 2008
    Posts
    199
    Thanks
    6
    Thanked 58 Times in 57 Posts

    Default

    (Using PHP 5)
    PHP Code:
    <?php
    #
    #CREATING THE HANDLER
    #
    $xml = new SimpleXMlElement('file.xml'0true);
    #
    #LOOPING THROUGH THE ITEM ELEMENTS
    #
    foreach($xml->item as $item){

    }
    #
    #GETTING AN ATTRIBUTE
    #
    foreach($xml->item as $item){
        echo 
    $item->attributes()->thumbimg;
    }
    #
    #SETTING AN ATTRIBUTE
    #
    foreach($xml->item as $item){
        
    $item->attributes()->thumbimg substr($item->attributes()->thumbimg0, -3) . 'png';
    }
    #
    #REMOVING THE FIRST ITEM
    #
    unset($xml->item[0]);
    #
    #ADDING AN ITEM
    #
    $itemHandler $xml->addChild('item');
    $itemHandler->addAttribute('thumbimg''newthumb.png');
    #
    #SAVING THE XML
    #
    file_put_contents('file.xml'$xml->asXml());

  3. #3
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  4. #4
    Join Date
    Dec 2008
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by techietim View Post
    (Using PHP 5)
    PHP Code:
    <?php
    #
    #CREATING THE HANDLER
    #
    $xml = new SimpleXMlElement('file.xml'0true);
    #
    #LOOPING THROUGH THE ITEM ELEMENTS
    #
    foreach($xml->item as $item){

    }
    #
    #GETTING AN ATTRIBUTE
    #
    foreach($xml->item as $item){
        echo 
    $item->attributes()->thumbimg;
    }
    #
    #SETTING AN ATTRIBUTE
    #
    foreach($xml->item as $item){
        
    $item->attributes()->thumbimg substr($item->attributes()->thumbimg0, -3) . 'png';
    }
    #
    #REMOVING THE FIRST ITEM
    #
    unset($xml->item[0]);
    #
    #ADDING AN ITEM
    #
    $itemHandler $xml->addChild('item');
    $itemHandler->addAttribute('thumbimg''newthumb.png');
    #
    #SAVING THE XML
    #
    file_put_contents('file.xml'$xml->asXml());
    Thanks a lot man. I am very grateful.

    But

    Code:
    REMOVING THE FIRST ITEM
    Code:
    unset($xml->item[0]);
    is not working.

    How can i delete a specific record from MY Formatted XML File. e.g.
    My XML File Format is

    <?xml version="1.0"?>
    <portfolio category="Category1">
    <item thumbimg="images/thumb1.jpg" largeimg1="images/large1_1.jpg" largeimg2="images/large1_2.jpg" largeimg3="images/large1_3.jpg" title="Item 1" type="Item 1 type" detail="Item 1 description" company="NEOVANTAGE" year="2002-03"/>
    <item thumbimg="images/thumb2.jpg" largeimg1="images/large2_1.jpg" largeimg2="images/large2_2.jpg" largeimg3="images/large2_3.jpg" title="Item 2" type="Item 2 type" detail="Item 2 description" company="NEOVANTAGE" year="2002-03"/>
    <item thumbimg="images/thumb3.jpg" largeimg1="images/large3_1.jpg" largeimg2="images/large3_2.jpg" largeimg3="images/large3_3.jpg" title="Item 3" type="Item 3 type" detail="Item 3 description" company="NEOVANTAGE" year="2002-03"/>
    <item thumbimg="images/thumb4.jpg" largeimg1="images/large4_1.jpg" largeimg2="images/large4_2.jpg" largeimg3="images/large4_3.jpg" title="Item 4" type="Item 4 type" detail="Item 4 Description" company="NEOVANTAGE" year="2008-09"/>
    </portfolio>

    Now i traverse these 4 records and showed in this format

    <table cellspacing="1" cellpadding="5" border="0" align="center" width="500">
    <tr>
    <td align="center" valign="middle" width="20" class="tab">ID</td>
    <td align="left" valign="middle" width="100" class="tab">Title</td>
    <td align="left" valign="middle" width="175" class="tab">Description</td>
    <td align="left" valign="middle" width="100" class="tab">Company</td>
    <td align="center" valign="middle" width="100" class="tab">Setting</td>
    </tr>
    <?
    //*********************** LOAD XML Document
    $xml = new SimpleXMlElement('../xml/1_2.xml', 0, true);
    $count=1;
    //*********************** Multiple Record
    foreach ($xml->portfolio as $folio){
    $category_name = $folio->attributes()->category;
    echo $category_name;
    }
    foreach ($xml->item as $item){
    $item_title = $item->attributes()->title;
    $item_detail = $item->attributes()->detail;
    $item_company = $item->attributes()->company;
    //$item_nodeValue = $node->nodeValue;
    ?>
    <tr>
    <td align='center' valign='top' class='text-gray'><?=$count;?></td>
    <td align='left' valign='top' class='text-gray'><?=$item_title;?></td>
    <td align='left' valign='top' class='text-gray'><div align="justify"><?=$item_detail;?></div></td>
    <td align='left' valign='top' class='text-gray'><?=$item_company;?></td>
    <td align='center' valign='top' class='text-gray'><a href='javascript: CautionFolioDelete(<?=$count;?>)'><img src='images/icons/del.jpg' width='14' height='17' border='0' alt='Delete' /></a></td>
    </tr>
    <? $count=$count+1;}?>
    </table>


    Now i want to delete the 3rd record. How can i delete the 3rd record considering i do not know how many records in my XML File but for the time being i have 4. So how can i delete the 3rd record. Awaiting of your reply and hope to get a good response again from your end.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •