note 86948 deleted from simplexmlelement.addAttribute by danbrown
| From: | danbrown@php.net | Date: | Fri, 28 Jan 2011 14:34:56 +0000 |
| Subject: | note 86948 deleted from simplexmlelement.addAttribute by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-176472@lists.php.net to get a copy of this message | ||
Note Submitter: khishigbaatar at 2xcoding dot com
----
simpleXML have only read/write methods, so to remove attributes in nodes, there is a simple way even
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.