PHP-XML createAttribute() Function Example

PHP-XML createAttribute() Function Example

Posted in : PHP Posted on : January 21, 2011 at 6:16 PM Comments : [ 0 ]

This section contains the detail about the XML createAttribute() Function Example in PHP.

PHP-XML createAttribute() Function Example:

The createAttribute("string") function take a string argument and create new attribute. Through this example we will create new attribute and append in the element in the xml file. we can also set attribute value.


$DOMObject = new DOMDocument("1.0");

header("Content-Type: text/plain");

$root_element = $DOMObject->createElement("root");


$root_item1 = $DOMObject->createElement("child1");

$root_item_text1 = $DOMObject->createTextNode("clied1 text");

$root_item1_attribute = $DOMObject->createAttribute("chield1_attribute");

$root_item1_attribute_value = $DOMObject->createTextNode("chield1 attribute value");

$root_item2 = $DOMObject->createElement("child2");

$root_item_text2 = $DOMObject->createTextNode("clied2 text");


echo $DOMObject->saveXML();

After running this example, the browser output like:

<?xml version="1.0"?>
    <child1 chield1_attribute="chield1 attribute value">clied1 text</child1>
    <child2>clied2 text</child2>
And the "root.xml" file save the location where you give in the system.

Download Example Code

Go to Topic «PreviousHomeNext»

Your Comment:

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image

Tutorial Topics