PHP Doku:: Create new attribute - domdocument.createattribute.html

Verlauf / Chronik / History: (1) anzeigen

Sie sind hier:
Doku-StartseitePHP-HandbuchFunktionsreferenzXML-ManipulationDocument Object ModelThe DOMDocument classDOMDocument::createAttribute

Ein Service von Reinhard Neidl - Webprogrammierung.

The DOMDocument class

<<DOMDocument::__construct

DOMDocument::createAttributeNS>>

DOMDocument::createAttribute

(PHP 5)

DOMDocument::createAttributeCreate new attribute

Beschreibung

DOMAttr DOMDocument::createAttribute ( string $name )

This function creates a new instance of class DOMAttr. Dieser Knoten wird in Ihrem Dokument nicht sichtbar sein, bis dieser zum Beispiel mit der Funktion DOMNode->appendChild() eingefügt wird.

Parameter-Liste

name

The name of the attribute.

Rückgabewerte

The new DOMAttr or FALSE if an error occured.

Fehler/Exceptions

DOM_INVALID_CHARACTER_ERR

Raised if name contains an invalid character.

Siehe auch


2 BenutzerBeiträge:
- Beiträge aktualisieren...
chandrachur at elegantsystems dot net
24.07.2008 9:41
A pretty simple example showing how to create attributes and add values to them:

<?php
$doc
= new DOMDocument('1.0', 'UTF-8');

$root = $doc->createElement('songs');
$doc->appendChild($root);

for(
$i=0;$i<10;$i++){

   
$root_child = $doc->createElement('song');
   
$root->appendChild($root_child);
   
   
$root_attr1 = $doc->createAttribute('url');
   
$root_child->appendChild($root_attr1);
   
   
$root_text = $doc->createTextNode('This is the root element!');
   
$root_attr1->appendChild($root_text);
   
   
$root_attr2= $doc->createAttribute('artist');
   
$root_child->appendChild($root_attr2);
   
   
$root_text = $doc->createTextNode('This is the root element!');
   
$root_attr2->appendChild($root_text);
   
   
$root_attr3 = $doc->createAttribute('track');
   
$root_child->appendChild($root_attr3);
   
   
$root_text = $doc->createTextNode('This is the root element!');
   
$root_attr3->appendChild($root_text);

}

print
$doc->saveXML();
?>

This will output as:

<?xml version="1.0" encoding="UTF-8" ?>
  <songs>
  <song url="This is the root element!" artist="This is the root element!" track="This is the root element!" />
  <song url="This is the root element!" artist="This is the root element!" track="This is the root element!" />
  <song url="This is the root element!" artist="This is the root element!" track="This is the root element!" />
  <song url="This is the root element!" artist="This is the root element!" track="This is the root element!" />
  <song url="This is the root element!" artist="This is the root element!" track="This is the root element!" />
  <song url="This is the root element!" artist="This is the root element!" track="This is the root element!" />
  <song url="This is the root element!" artist="This is the root element!" track="This is the root element!" />
  <song url="This is the root element!" artist="This is the root element!" track="This is the root element!" />
  <song url="This is the root element!" artist="This is the root element!" track="This is the root element!" />
  <song url="This is the root element!" artist="This is the root element!" track="This is the root element!" />
  </songs>
boen dot robot at the-google-mail dot com
12.11.2006 19:43
If you're looking for an easy way to create an attribute with a certain value, in a similar to createElement() manner, you should use the setAttribute() instead. Documentation and example is available below:
http://php.net/manual/en/domdocument.setattribute.php



PHP Powered Diese Seite bei php.net
The PHP manual text and comments are covered by the Creative Commons Attribution 3.0 License © the PHP Documentation Group - Impressum - mail("TO:Reinhard Neidl",...)