note 86948 added to function.simplexml-element-addAttribute
| From: | khishigbaatar at 2xcoding dot com | Date: | Wed, 12 Nov 2008 02:44:34 +0000 |
| Subject: | note 86948 added to function.simplexml-element-addAttribute | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-146407@lists.php.net to get a copy of this message | ||
simpleXML have only read/write methods. Then how to remove attributes in node?
There is a simpe way to remove attribute without DOM extension. Use unset();
Example:
if "samlpe.xml" contains
<?xml version="1.0"?>
<books>
<book author="unknown">The Secret History of the Mongols</book>
<book author="Jack Weatherford">Genghis Khan and the Making of the Modern
World</book>
<book author="David Morgan">The Mongols (The Peoples of Europe)</book>
<book author="Stephen Turnbull">Mongol Warrior 1200-1350</book>
<book author="Marco Polo">The Travels of Marco Polo</book>
<book author="J. J. Saunders">The History of Mongol Conquests</book>
</books>
<?php
// load xml file
$xml = simplexml_load_file("sample.xml");
// remove first book's attribute
unset($xml->book[0][author]);
// write xml file
$xml->asXML("sample.xml");
?>
Now "sample.xml" is:
<?xml version="1.0"?>
<books>
<book>The Secret History of the Mongols</book>
<book author="Jack Weatherford">Genghis Khan and the Making of the Modern
World</book>
<book author="David Morgan">The Mongols (The Peoples of Europe)</book>
<book author="Stephen Turnbull">Mongol Warrior 1200-1350</book>
<book author="Marco Polo">The Travels of Marco Polo</book>
<book author="J. J. Saunders">The History of Mongol Conquests</book>
</books>
That's it.
----
Server IP: 219.94.145.73
Probable Submitter: 210.157.228.36
----
Manual Page -- http://www.php.net/manual/en/function.simplexml-element-addAttribute.php
Edit -- https://master.php.net/note/edit/86948
Del: integrated -- https://master.php.net/note/delete/86948/integrated
Del: useless -- https://master.php.net/note/delete/86948/useless
Del: bad code -- https://master.php.net/note/delete/86948/bad+code
Del: spam -- https://master.php.net/note/delete/86948/spam
Del: non-english -- https://master.php.net/note/delete/86948/non-english
Del: in docs -- https://master.php.net/note/delete/86948/in+docs
Del: other reasons-- https://master.php.net/note/delete/86948
Reject -- https://master.php.net/note/reject/86948
Search -- https://master.php.net/manage/user-notes.php