(PHP 5 >= 5.1.3)
SimpleXMLElement::addAttribute — Adds an attribute to the SimpleXML element
$name
[, string $value
[, string $namespace
]] )Adds an attribute to the SimpleXML element.
name
The name of the attribute to add.
value
The value of the attribute.
namespace
If specified, the namespace to which the attribute belongs.
没有返回值。
Note:
Listed examples may include example.php, which refers to the XML string found in the first example of the basic usage guide.
Example #1 Add attributes and children to a SimpleXML element
<?php
include 'example.php';
$sxe = new SimpleXMLElement($xmlstr);
$sxe->addAttribute('type', 'documentary');
$movie = $sxe->addChild('movie');
$movie->addChild('title', 'PHP2: More Parser Stories');
$movie->addChild('plot', 'This is all about the people who make it work.');
$characters = $movie->addChild('characters');
$character = $characters->addChild('character');
$character->addChild('name', 'Mr. Parser');
$character->addChild('actor', 'John Doe');
$rating = $movie->addChild('rating', '5');
$rating->addAttribute('type', 'stars');
echo $sxe->asXML();
?>
以上例程的输出类似于:
<?xml version="1.0" standalone="yes"?> <movies type="documentary"> <movie> <title>PHP: Behind the Parser</title> <characters> <character> <name>Ms. Coder</name> <actor>Onlivia Actora</actor> </character> <character> <name>Mr. Coder</name> <actor>El ActÓr</actor> </character> </characters> <plot> So, this language. It's like, a programming language. Or is it a scripting language? All is revealed in this thrilling horror spoof of a documentary. </plot> <great-lines> <line>PHP solves all my web problems</line> </great-lines> <rating type="thumbs">7</rating> <rating type="stars">5</rating> </movie> <movie> <title>PHP2: More Parser Stories</title> <plot>This is all about the people who make it work.</plot> <characters> <character> <name>Mr. Parser</name> <actor>John Doe</actor> </character> </characters> <rating type="stars">5</rating> </movie> </movies>
p.servus (2011-11-18 17:42:48)
If you need an Namespace, you must write a Prefix of the Namespace + the Name of the Attribute ("prefix:name") as the first parameter AND the uri as the third one. (I don't know why?!)
Example code:
<?php
$xml = new SimpleXMLElement("<packagedElement></packagedElement>");
$xml->addAttribute("xmi:type", "uml:Class", "http://schema.omg.org/spec/XMI/2.1");
echo $xml->asXml();
?>
Output:
<packagedElement xmlns:xmi="http://schema.omg.org/spec/XMI/2.1" xmi:type="uml:Class"></packagedElement>
sarlak (2011-05-07 07:49:11)
You can access a node child with his name this way :
<?php
$root = new SimpleXMLElement($filePath);
echo $root->nodeName->attributes;
// It works recursivly so this will work too
echo $root->nodeName->subNodeName->attributes();
?>
But if you want to ADD an attribute to a children you MUST use the children() method to access and modfiy it or it will modify the parent's attributes.
<?php
// Add a subnode
$element->nodeName->addChild('subNodeName', "whatever you want");
// Get the pos in the childrens of the parent node
$lastNodePos = $element->nodeName->count()-1;
// Get the parents node childrens
$nodeChildrens = $element->nodeName->children();
// Add an attribute to the last created
$nodeChildrens[$lastNodePos]->addAttribute('attributeName', "The attribute value);
?>
booleer at yahoo dot it (2010-12-16 09:44:56)
If the attribute already exists, addAttribute does nothing.
example:
<?php
$xml_string = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<root>
<item id="foo">
<root>
XML;
$xml = simplexml_load_string($xml_string);
$xml->item->addAttribute('id', 'bar');
echo $xml->asXML();
?>
The above examples will output:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<item id="foo">
<root>