View Full Version : How to read and write the XML DOM with PHP
neovantage
12-30-2008, 09:17 AM
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
techietim
12-30-2008, 04:28 PM
(Using PHP 5)
<?php
#
#CREATING THE HANDLER
#
$xml = new SimpleXMlElement('file.xml', 0, true);
#
#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()->thumbimg, 0, -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());
neovantage
12-31-2008, 12:16 PM
(Using PHP 5)
<?php
#
#CREATING THE HANDLER
#
$xml = new SimpleXMlElement('file.xml', 0, true);
#
#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()->thumbimg, 0, -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
REMOVING THE FIRST ITEM
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.
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.