PHP SimpleXMLElement::addAttribute SimpleXML 函数

  • 定义和用法

    SimpleXMLElement::addAttribute - 向SimpleXML元素添加属性
  • 版本支持

    PHP4 PHP5 PHP7
    不支持 v5.1.3+支持 支持
  • 语法

    SimpleXMLElement::addAttribute ( string $name [, string $value [, string $namespace ]] )
  • 参数

    参数 必需的 描述
    name 要添加的属性的名称。。
    value 属性的值。
    namespace 如果指定,则属性所属的命名空间。
  • 返回值

  • 示例

    $xmlstr = <<<XML
    <?xml version='1.0' standalone='yes'?>
      <title>PHP: Behind the Parser</title>
        <name>Ms. Coder</name>
        <actor>Onlivia Actora</actor>
        <name>Mr. Coder</name>
        <actor>El ActÓr</actor>
       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.
       <line>PHP solves all my web problems</line>
      <rating type="thumbs">7</rating>
      <rating type="stars">5</rating>
    $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();
  • 相关函数

    SimpleXMLElement::addChild() - 将子元素添加到XML节点