note 86948 modified in function.simplexml-element-addAttribute by danbrown
| From: | danbrown@php.net | Date: | Wed, 16 Sep 2009 15:48:12 +0000 |
| Subject: | note 86948 modified in function.simplexml-element-addAttribute by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-160666@lists.php.net to get a copy of this message | ||
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.
--was--
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.
http://php.net/manual/en/function.simplexml-element-addAttribute.php