473,385 Members | 1,397 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

How to read and write the XML DOM with PHP

245 100+
Hey,
I am working on reading and writing the XML DOM File using PHP.
My XML File is In this Format

Expand|Select|Wrap|Line Numbers
  1. portfolio category="Category1">
  2.     <item thumbimg="http://bytes.com/images/thumb1.jpg" largeimg1="http://bytes.com/images/large1_1.jpg" largeimg2="http://bytes.com/images/large1_2.jpg" largeimg3="http://bytes.com/images/large1_3.jpg" title="Item 1" type="Item 1 type" detail="Item 1 description" company="Invent Solution" year="2002-03"  />
  3.     <item thumbimg="http://bytes.com/images/thumb1.jpg" largeimg1="http://bytes.com/images/large1_1.jpg" largeimg2="http://bytes.com/images/large1_2.jpg" largeimg3="http://bytes.com/images/large1_3.jpg" title="Item 2" type="Item 2 type" detail="Item 2 description" company="Invent Solution 2" year="2007-08" />
  4. </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
Dec 30 '08 #1
25 3991
code green
1,726 Expert 1GB
What problem are you having?
Dec 30 '08 #2
Dormilich
8,658 Expert Mod 8TB
option 1 – PHP: DOMDocument - Manual
(esp. the loadXML() and save() methods, XML manipulation is done with the standard DOM functions)

option 2 – PHP: SimpleXML - Manual
(good for reading simple xml documents)

option 3 – PHP: XSL - Manual
(applying all changes in one go, XSLT knowledge (some) required)

regards
Dec 30 '08 #3
neovantage
245 100+
My Problem is that how can i read and write the above formatted XML file using PHP, edit the specific existing record and delete.
Dec 30 '08 #4
Dormilich
8,658 Expert Mod 8TB
that depends on you specific task. there might be situations when it's better to do XSLT (xml structure changes) and some where a simple DOM method will do.

as for loading, changing and saving your xml, read the manuals. they explain it pretty straightforward.

regards
Dec 30 '08 #5
neovantage
245 100+
The thing is my XML is not in the format as the examples given using SimpleXML or the examples given in PHP: DOMDocument. Can you write a sample code for me that how can i read this formatted document and add a new record or edit existing record using PHP.
Dec 30 '08 #6
Dormilich
8,658 Expert Mod 8TB
have you actually tried to load your xml? (the samples are samples after all.... my own xml files are different too, they even have namespace, but they work)

if it doesn't work, what error message do you get?
Dec 30 '08 #7
neovantage
245 100+
Yeh i have tried but it shows nothing, here is my code
Expand|Select|Wrap|Line Numbers
  1. $doc = new DOMDocument();
  2. $doc->load('../xml/1.xml');
  3. $portfolio = $doc->getElementsByTagName( "portfolio" );
  4. foreach( $portfolio as $row ){
  5. $items = $row->getElementsByTagName( "item" );
  6.                                                                                      $item = $items->item(0)->nodeValue;
  7.                                                                                     echo $item;
  8.                                                                                }
  9.  
My loaded XML document is in this format
Expand|Select|Wrap|Line Numbers
  1.  <portfolio category="Category1">
  2.      <item thumbimg="http://bytes.com/images/thumb1.jpg" largeimg1="http://bytes.com/images/large1_1.jpg" largeimg2="http://bytes.com/images/large1_2.jpg" largeimg3="http://bytes.com/images/large1_3.jpg" title="Item 1" type="Item 1 type" detail="Item 1 description" company="Invent Solution" year="2002-03"  />
  3.      <item thumbimg="http://bytes.com/images/thumb1.jpg" largeimg1="http://bytes.com/images/large1_1.jpg" largeimg2="http://bytes.com/images/large1_2.jpg" largeimg3="http://bytes.com/images/large1_3.jpg" title="Item 2" type="Item 2 type" detail="Item 2 description" company="Invent Solution 2" year="2007-08" />
  4.  </portfolio>
  5.  
Dec 30 '08 #8
Dormilich
8,658 Expert Mod 8TB
ok, that's something to work with.

do you know if you actually enter the foreach loop? (i.e. if you have a non-empty result set)

why using getElementsByTagName() on <portfolio>? that seems unnecessary to me.

regards

PS: what do you expect item(0)->nodeValue to be? you're requesting the value of the element node (which in DOM is strictly speaking non-existant) you could get the value of the text node (which would be $node->firstChild->nodeValue). I think you get the text node's value................ of an empty element!

try
Expand|Select|Wrap|Line Numbers
  1. var_dump($item);
Dec 30 '08 #9
neovantage
245 100+
@Dormilich
i am using getElementsByTagName() on <portfolio> because i need to read the category name and as you can see the tag <item> has element title and title value but i am unable to read it. How can i read the title, type, detail, company, year, thumbimg, largeimg1, largeimg2, largeimg3 values? and how can i add a new record which append at the end of my alredy exisiting record
Dec 30 '08 #10
Dormilich
8,658 Expert Mod 8TB
@neovantage
DOMElement::getAttribute();

@neovantage
DOMDocument::createElement();
DOMNode::appendChild();

basicly, standard DOM procedures, only they are splitted over several DOM XML classes
Dec 30 '08 #11
neovantage
245 100+
i am unable to use that function as i need an example script which use DOMElement::getAttribute(); because i am not that much professional to get the picture of just having the function and performed further operations. Kindly help me out i really need to know about it.
Dec 30 '08 #12
Dormilich
8,658 Expert Mod 8TB
let's make an example.

get the category value (tested and working):
Expand|Select|Wrap|Line Numbers
  1. $doc = new DOMDocument();
  2. $doc->load($xml); // $xml is your xml file
  3. $portfolio = $doc->getElementsByTagName( "portfolio" );
  4. $item = $portfolio->item(0)->getAttribute('category');
  5. // prints: Category1
  6. echo $item;
PS: if you know how to use DOM in Javascript, this is pretty much the same

regards
Dec 30 '08 #13
neovantage
245 100+
@Dormilich
Thank you very very very muchhhhhhhhhhh.
It works now i done the same thing with the inner tag and it is working that's cool

My Next question is that " How can i traverse whole recodes so that i can show e.g. when we write the db query then we can fetch array by using while condition with mysql_fetch_array(records) and it automatically terminates once it will be at last record. So is there any method here in XML that i can fetch all the records and it terminate automatically once the last record found.
Dec 30 '08 #14
Dormilich
8,658 Expert Mod 8TB
@neovantage
Expand|Select|Wrap|Line Numbers
  1. foreach (DOMNodeList $list as DOMNode $node)
  2. {
  3.     // code comes here
  4. }
note that I put in the class names to make clear, what types $list & $node are, of course you call foreach as you usually do.

in some cases you can also use the while loop, depending on the exit definition.

regards
Dec 30 '08 #15
neovantage
245 100+
@Dormilich
Sorry i did not get you. i did not get in my XML Document case what is my DOMNodeList $list and what is my DOMNode $node.

I successfully got each attribute and here is my code
Expand|Select|Wrap|Line Numbers
  1. $doc = new DOMDocument();
  2. $doc->load('../xml/1.xml'); // $xml is your xml file
  3. $portfolio = $doc->getElementsByTagName( "portfolio" );
  4. $category_name = $portfolio->item(0)->getAttribute('category');
  5. $item = $doc->getElementsByTagName( "item" );
  6. $item_title = $item->item(0)->getAttribute('title');
  7. $item = $doc->getElementsByTagName( "item" );
  8. $item_detail = $item->item(0)->getAttribute('detail');
  9. $item = $doc->getElementsByTagName( "item" );
  10. $item_company = $item->item(0)->getAttribute('company');
  11.  

and my XML Document is
Expand|Select|Wrap|Line Numbers
  1. <portfolio category="Category1">
  2.     <item thumbimg="http://bytes.com/images/thumb1.jpg" largeimg1="http://bytes.com/images/large1_1.jpg" largeimg2="http://bytes.com/images/large1_2.jpg" largeimg3="http://bytes.com/images/large1_3.jpg" title="Item 1" type="Item 1 type" detail="Item 1 description" company="Invent Solution" year="2002-03"  />
  3.     <item thumbimg="http://bytes.com/images/thumb1.jpg" largeimg1="http://bytes.com/images/large1_1.jpg" largeimg2="http://bytes.com/images/large1_2.jpg" largeimg3="http://bytes.com/images/large1_3.jpg" title="Item 2" type="Item 2 type" detail="Item 2 description" company="Invent Solution 2" year="2007-08" />
  4.     <item thumbimg="http://bytes.com/images/thumb1.jpg" largeimg1="http://bytes.com/images/large1_1.jpg" largeimg2="http://bytes.com/images/large1_2.jpg" largeimg3="http://bytes.com/images/large1_3.jpg" title="Item 3" type="Item 3 type" detail="Item 3 description" company="Invent Solution 3"  year="2008-09" />
  5. </portfolio>
  6.  

Now i need to write the foreach loop for this that it traverse from very first record to last record. You told me the method
Expand|Select|Wrap|Line Numbers
  1.    1. foreach (DOMNodeList $list as DOMNode $node)
  2.    2. {
  3.    3.     // code comes here
  4.    4. }
  5.  
so i want to know that what is my DOMNodeList $list and what is DOMNode $node ?
Dec 30 '08 #16
neovantage
245 100+
@Dormilich
I am unable to write a foreach loop in order to traverse all records. Kindly help me out to write a foreach loop in order to traverse all records.

In order to fetch a single record this code i working fine
Expand|Select|Wrap|Line Numbers
  1. $doc = new DOMDocument();
  2. $doc->load('../xml/1.xml'); // $xml is your xml file
  3.  
  4. $portfolio = $doc->getElementsByTagName( "portfolio" );
  5. $category_name = $portfolio->item(0)->getAttribute('category');
  6.  
  7. $item = $doc->getElementsByTagName( "item" );
  8. $item_title = $item->item(0)->getAttribute('title');
  9.  
  10. $item = $doc->getElementsByTagName( "item" );
  11. $item_detail = $item->item(0)->getAttribute('detail');
  12.  
  13. $item = $doc->getElementsByTagName( "item" );
  14. $item_company = $item->item(0)->getAttribute('company');
  15.  
But in order to fetch each record i wrote foreach loop and it is not working
here is my code
Expand|Select|Wrap|Line Numbers
  1. <?
  2.                                                                                 $doc = new DOMDocument();
  3.                                                                                 $doc->load('../xml/1.xml'); // This is my xml file
  4.                                                                                 $portfolio = $doc->getElementsByTagName( "portfolio" );
  5.                                                                                 $category_name = $portfolio->item(0)->getAttribute('category');
  6.  
  7.                                                                                 $item = $doc->getElementsByTagName( "item" );
  8.                                                                                 foreach ($item as $value){
  9.                                                                                     $items = $value->getElementsByTagName( "item" );
  10.                                                                                     $item_title = $items->item(0)->getAttribute('title');
  11.  
  12.                                                                                     $items = $value->getElementsByTagName( "item" );
  13.                                                                                     $item_detail = $itemNode->item(0)->getAttribute('detail');
  14.  
  15.                                                                                     $items = $value->getElementsByTagName( "item" );
  16.                                                                                     $$item_company = $itemNode->item(0)->getAttribute('company');
  17.                                                                                 ?>
  18.                                                                                 <tr class=$class>
  19.                                                                                     <td align='center' valign='top' class='text-gray'>1</td>
  20.                                                                                     <td align='left' valign='top' class='text-gray'><?=$category_name;?></td>
  21.                                                                                     <td align='left' valign='top' class='text-gray'><?=$item_title;?></td>
  22.                                                                                     <td align='left' valign='top' class='text-gray'><div align="justify"><?=$item_detail;?></div></td>
  23.                                                                                     <td align='left' valign='top' class='text-gray'><?=$item_company;?></td>
  24.                                                                                 </tr>
  25.                                                                                 <? }?>
  26.  
Kindly help me out to resolve my problem.
Dec 30 '08 #17
Dormilich
8,658 Expert Mod 8TB
@neovantage
how to read the manual:
// this is a method definition
DOMNodeList DOMDocument::getElementsByTagName ( string $name )
first value: return type (DOMNodeList)
second value (before ::): class name (DOMDocument)
second value (after ::): method name (getElementsByTagName)
third value: 1st parameter's type (string)
fourth value: 1st parameter (usually a variable with a descriptive name)

so what does it tell you:
you can use the method getElementsbyTagName on every object that is a DOMDocument (or to be more precise, an instance of the DOMDocument class. note that methods can be inherited!).

the return value of that method is a DOMNodeList (a DOMNodeList instance) which means that you can only apply the methods/properties of the DOMNodeList to the new variable holding the return value.

I don't think I have to discuss parameters....

in the manual every method and every property has such a description. now you can look up all the used methods and see how they correspond.

or specifically in this case:
$doc->getElementsByTagName($name) returns a DOMNodeList instance. (which I named $list) and $node is (as usual in foreach) the variable to work with (in your example it would be a DOMElement instance which is a descendand of the DOMNode class)

to see what variable belongs to what class use var_dump()
Dec 30 '08 #18
neovantage
245 100+
@Dormilich
Now i got it and i did that the same thing now but it again not working.
Here is my code
Expand|Select|Wrap|Line Numbers
  1. $doc = new DOMDocument();
  2. $doc->load('../xml/1.xml'); // My xml file
  3.  
  4. $portfolio = $doc->getElementsByTagName( "portfolio" );
  5. $category_name = $portfolio->item(0)->getAttribute('category');
  6.  
  7. $item_list = $doc->getElementsByTagName( "item" );
  8.  
  9. foreach ($item_list as $node){
  10.                                                                 $item_title = $node->item(0)->getAttribute('title');                            $item_detail = $node->item(0)->getAttribute('detail');                        $item_company = $node->item(0)->getAttribute('company');
  11. ?>
  12. <tr>
  13.                                                                                     <td align='center' valign='top' class='text-gray'>1</td>
  14.                                                                                     <td align='left' valign='top' class='text-gray'><?=$category_name;?></td>
  15.                                                                                     <td align='left' valign='top' class='text-gray'><?=$item_title;?></td>
  16.                                                                                     <td align='left' valign='top' class='text-gray'><div align="justify"><?=$item_detail;?></div></td>
  17.                                                                                     <td align='left' valign='top' class='text-gray'><?=$item_company;?></td>
  18.                                                                                 </tr>
  19.                                                                                 <? }?>
  20.  
But this code is not working and keep on giving me the error
Fatal error: Call to undefined method DOMElement::item()



This is my xml File Format
Expand|Select|Wrap|Line Numbers
  1. <portfolio category="Category1">
  2.     <item thumbimg="http://bytes.com/images/thumb1.jpg" largeimg1="http://bytes.com/images/large1_1.jpg" largeimg2="http://bytes.com/images/large1_2.jpg" largeimg3="http://bytes.com/images/large1_3.jpg" title="Item 1" type="Item 1 type" detail="Item 1 description" company="Invent Solution" year="2002-03"  />
  3.     <item thumbimg="http://bytes.com/images/thumb1.jpg" largeimg1="http://bytes.com/images/large1_1.jpg" largeimg2="http://bytes.com/images/large1_2.jpg" largeimg3="http://bytes.com/images/large1_3.jpg" title="Item 2" type="Item 2 type" detail="Item 2 description" company="Invent Solution 2" year="2007-08" />
  4. </portfolio>
  5.  

Awaitinggggggggggggg!!!! Please help me out
Dec 30 '08 #19
Dormilich
8,658 Expert Mod 8TB
@neovantage
of course. as stated, $node is a DOMElement instance, thus item() does not work on it (item() works on DOMNodeList instances). just omit the item(0) call. the foreach construct takes care of the type conversion (in this case from DOMNodeList to DOMElement)
Expand|Select|Wrap|Line Numbers
  1. $item_title = $node->getAttribute('title');
and please use [code] tags when posting code

PS: to see it logically: what type do you get for each single member of a DOMNodeList (a kind of array)? a DOMNode or one of its descendands (all DOM classes except Exception, Implementation, NamedNodemap, Notation, NodeList and XPath)
Dec 30 '08 #20
neovantage
245 100+
@Dormilich
Thank you very very very very very much again. It works.

What my next task is about to append a new record at the end of my last record.
As you know my format of XML File so how do i add a new record? Awaiting of your reply.
Dec 30 '08 #21
Dormilich
8,658 Expert Mod 8TB
@neovantage
I advice you to read a DOM tutorial (it doesn't matter if it is a Javascript tutorial about DOM scripting) e.g. XML DOM Introduction (you can skip the part about loading the xml.)

some differences between JS DOM and PHP DOM:
object.method() vs. $object->method()
NodeList[i] vs. $NodeList->item($i)

remember: both languages use the same API
Dec 30 '08 #22
Atli
5,058 Expert 4TB
neovantage,

Please use [code] tags when posting your code examples.
(See How to ask a question)

[code] ... Code goes here... [/code]

Thank you.
Moderator
Dec 30 '08 #23
neovantage
245 100+
@Dormilich
Hey,

I am stuck again while deleting a node using simpleXML.

This is my Formated XML Document

Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0"?>
  2. <portfolio category="Category1">
  3.     <item thumbimg="http://bytes.com/images/thumb1.jpg" largeimg1="http://bytes.com/images/large1_1.jpg" largeimg2="http://bytes.com/images/large1_2.jpg" largeimg3="http://bytes.com/images/large1_3.jpg" title="Item 1" type="Item 1 type" detail="Item 1 description" company="Invent Solution" year="2002-03"  />
  4.     <item thumbimg="http://bytes.com/images/thumb2.jpg" largeimg1="http://bytes.com/images/large1_1.jpg" largeimg2="http://bytes.com/images/large1_2.jpg" largeimg3="http://bytes.com/images/large1_3.jpg" title="Item 2" type="Item 2 type" detail="Item 2 description" company="Invent Solution 2" year="2007-08" />
  5.     <item thumbimg="http://bytes.com/images/thumb3.jpg" largeimg1="http://bytes.com/images/large1_1.jpg" largeimg2="http://bytes.com/images/large1_2.jpg" largeimg3="http://bytes.com/images/large1_3.jpg" title="Item 3" type="Item 3 type" detail="Item 3 description" company="Invent Solution 3"  year="2008-09" />
  6.     <item thumbimg="http://bytes.com/images/thumb4.jpg" largeimg1="http://bytes.com/images/large1_1.jpg" largeimg2="http://bytes.com/images/large1_2.jpg" largeimg3="http://bytes.com/images/large1_3.jpg" title="Item 4" type="Item 1 type" detail="Item 1 description" company="Invent Solution" year="2002-03" /> 
  7.     <item thumbimg="http://bytes.com/images/thumb5.jpg" largeimg1="http://bytes.com/images/large1_1.jpg" largeimg2="http://bytes.com/images/large1_2.jpg" largeimg3="http://bytes.com/images/large1_3.jpg" title="Item 5" type="Item 1 type" detail="Item 1 description" company="Invent Solution" year="2002-03"  />
  8.     <item thumbimg="http://bytes.com/images/thumb6.jpg" largeimg1="http://bytes.com/images/large1_1.jpg" largeimg2="http://bytes.com/images/large1_2.jpg" largeimg3="http://bytes.com/images/large1_3.jpg" title="Item 6" type="Item 1 type" detail="Item 1 description" company="Invent Solution" year="2002-03" />
  9.     <item thumbimg="http://bytes.com/images/thumb7.jpg" largeimg1="http://bytes.com/images/large1_1.jpg" largeimg2="http://bytes.com/images/large1_2.jpg" largeimg3="http://bytes.com/images/large1_3.jpg" title="Item 7" type="Item 1 type" detail="Item 1 description" company="Invent Solution"  year="2002-03" />
  10.     <item thumbimg="http://bytes.com/images/thumb8.jpg" largeimg1="http://bytes.com/images/large1_1.jpg" largeimg2="http://bytes.com/images/large1_2.jpg" largeimg3="http://bytes.com/images/large1_3.jpg" title="Item 8" type="Item 1 type" detail="Item 1 description" company="Invent Solution" year="2002-03" /> 
  11.     <item thumbimg="http://bytes.com/images/thumb9.jpg" largeimg1="http://bytes.com/images/large1_1.jpg" largeimg2="http://bytes.com/images/large1_2.jpg" largeimg3="http://bytes.com/images/large1_3.jpg" title="Item 9" type="Item 1 type" detail="Item 1 description" company="Invent Solution" year="2002-03"  />      
  12. </portfolio>

This is how i traverse my XML Document


Expand|Select|Wrap|Line Numbers
  1. <table cellspacing="1" cellpadding="5" border="0" align="center" width="500">
  2.                                                                                 <tr>
  3.                                                                                     <td align="center" valign="middle" width="20" class="tab">ID</td>
  4.                                                                                     <td align="left" valign="middle" width="100" class="tab">Title</td>
  5.                                                                                     <td align="left" valign="middle" width="175" class="tab">Description</td>
  6.                                                                                     <td align="left" valign="middle" width="100" class="tab">Company</td>
  7.                                                                                     <td align="center" valign="middle" width="100" class="tab">Setting</td>
  8.                                                                                 </tr>
  9.                                                                                 <?
  10.                                                                                 //*********************** LOAD XML Document
  11.                                                                                 $xml = new SimpleXMlElement('../xml/1.xml', 0, true);
  12.                                                                                 $count=1;
  13.                                                                                 //*********************** Multiple Record
  14.                                                                                 foreach ($xml->portfolio as $folio){
  15.                                                                                     $category_name = $folio->attributes()->category;
  16.                                                                                     echo $category_name;
  17.                                                                                 }
  18.                                                                                 foreach ($xml->item as $item){
  19.                                                                                     //unset($item);
  20.                                                                                     $item_title = $item->attributes()->title;
  21.                                                                                     $item_detail = $item->attributes()->detail;
  22.                                                                                     $item_company = $item->attributes()->company; 
  23.                                                                                     $item_thumbimg = $item->attributes()->thumbimg; 
  24.                                                                                     //$item_nodeValue = $node->nodeValue;
  25.                                                                                 ?>
  26.                                                                                 <tr>
  27.                                                                                     <td align='center' valign='top' class='text-gray'><?=$count;?></td>
  28.                                                                                     <td align='left' valign='top' class='text-gray'><?=$item_title;?></td>
  29.                                                                                     <td align='left' valign='top' class='text-gray'><div align="justify"><?=$item_detail;?></div></td>
  30.                                                                                     <td align='left' valign='top' class='text-gray'><?=$item_company;?></td>
  31.                                                                                     <td align='center' valign='top' class='text-gray'><a href="javascript: CautionFolioDelete('<?=$item_thumbimg;?>')"><img src='images/icons/del.jpg' width='14' height='17' border='0' alt='Delete' /></a></td>
  32.                                                                                 </tr>
  33.                                                                                 <? $count=$count+1;}?>
  34.                                                                             </table>
  35.  


now i want to delete the 4th record. I started a counter named with the variable "$count" and write a javascript function which take "thumbimg" argument and submit the data using Get Technique.

Now when my page is submitted. i wrote code like this given below

Expand|Select|Wrap|Line Numbers
  1. if(isset($_GET['folio_id'])){
  2.         $folio_id=$_GET['folio_id'];
  3.         $xml = new SimpleXMlElement('../xml/1.xml', 0, true);
  4.         foreach($xml->item as $item){
  5.             if($item['thumbimg'] == $folio_id){
  6.                 //unset($item);
  7.                 unset($item->item); // One method to delete the record
  8.  
  9.                 // Other Method to delete the record
  10.                 $item=dom_import_simplexml($item);
  11.                 $item->parentNode->removeChild($item);
  12.             }
  13.         }
  14.         echo $xml->asXml();
  15.     }
  16.  
  17.  
It do not delete my record. is there any kinda error in my script while deleting the specific record?


Best regards,
NEOVANTAGE
Dec 31 '08 #24
Dormilich
8,658 Expert Mod 8TB
@neovantage
several:
- SimpleXML can't delete nodes
- unset($item) only deletes the temporary variable $item
- dom_import_simplexml() returns a DOMElement, the original SimpleXML isn't affected by that

the only option I see is using DOMDocument from the beginning.

regards
Dec 31 '08 #25
neovantage
245 100+
@Dormilich
Thanks Brother,
It's done. now i can delete the record using
Expand|Select|Wrap|Line Numbers
  1. if(isset($_GET['folio_id'])){
  2.         $folio_id=$_GET['folio_id'];
  3.         $xml = new SimpleXMlElement('../xml/1.xml', 0, true);
  4.         foreach($xml->item as $item){
  5.             if($item['thumbimg'] == $folio_id){
  6.                 $item=dom_import_simplexml($item);
  7.                 $item->parentNode->removeChild($item);
  8.             }
  9.         }
  10.         $xml->asXml('../xml/1.xml');
  11.     }
  12.  
The only error was i was writing only
Expand|Select|Wrap|Line Numbers
  1. $xml->asXml();
insted of
Expand|Select|Wrap|Line Numbers
  1. $xml->asXml('../xml/1.xml');
Dec 31 '08 #26

Sign in to post your reply or Sign up for a free account.

Similar topics

18
by: jas | last post by:
Hi, I would like to start a new process and be able to read/write from/to it. I have tried things like... import subprocess as sp p = sp.Popen("cmd.exe", stdout=sp.PIPE)...
6
by: BBM | last post by:
I have an object that has a fairly complex construction sequence, so I have written a dedicated "factory" class that invokes the constructor of my object class (which does nothing but instantiate...
6
by: Juan Manuel Ramollino | last post by:
Greetings everyone! I am creating a webcontrol that reads one or more directorie's content and displays all the filenames (tipically PDFs or PPTs) so that the user can select the desired one and...
5
by: Just Me | last post by:
Using streams how do I write and then read a set of variables? For example, suppose I want to write into a text file: string1,string2,string3 Then read them later. Suppose I want to write...
8
by: a | last post by:
I have a struct to write to a file struct _structA{ long x; int y; float z; } struct _structA A; //file open write(fd,A,sizeof(_structA)); //file close
1
by: Arpan | last post by:
The contents of a text file are as follows: The Quick Brown Fox Jumped Over The Lazy Dog. Note that there isn't any space at the end of each of the 3 lines. Now when I do this:
8
by: dosworldguy | last post by:
I have been having a very peculiar issue from a long time. I have an application where multiple clients read from a shared set of files. When a record is changed, sometimes the win9x clients...
1
by: vinothg | last post by:
I have a binary file,which contains strings of 30 bytes each.I need to open the file,read the strings one by one and if the string is not found i need to write it.But unfortunately both read and...
23
by: asit dhal | last post by:
hello friends, can anyone explain me how to use read() write() function in C. and also how to read a file from disk and show it on the monitor using onlu read(), write() function ??????
9
by: vineeth | last post by:
Hello all, I have come across a weird problem, I need to determine the amount of bytes read from a file, but couldn't figure it out , My program does this : __ file = open("somefile") data =...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.