PHP addChild() Function

In this chapter you will learn:

  1. Description for PHP addChild() Function
  2. Syntax for PHP addChild() Function
  3. Parameter for PHP addChild() Function
  4. Return for PHP addChild() Function
  5. Example - adds a child element to the SimpleXML element

Description

The addChild() function adds a child element to the SimpleXML element.

Syntax

PHP addChild() Function has the following syntax.

addChild(name,value,ns);

Parameter

ParameterIs RequiredDescription
nameRequired.Name of the child element to add
valueOptional.Value of the child element
nsOptional.A namespace for the child element

Return

Returns a SimpleXMLElement object that represents the child added to the XML.

Example

Add a child element to the body element and a footer element:


<?php//from   j a v a 2 s.c o  m
$note=<<<XML
<book>
    <name>PHP</name>
    <name>Java</name>
</book>
XML;

$xml=new SimpleXMLElement($note);
$xml->body->addChild("date","2013-01-01");
$footer=$xml->addChild("Chapter","Some Chapter");

echo $xml->asXML();
?>

The code above generates the following result.

Next chapter...

What you will learn in the next chapter:

  1. Description for PHP asXML() Function
  2. Syntax for PHP asXML() Function
  3. Parameter for PHP asXML() Function
  4. Return for PHP asXML() Function
  5. Example - Format the SimpleXML object's data in XML
Home » PHP Tutorial » PHP SimpleXML Functions
PHP SimpleXMLElement Create
PHP addAttribute() Function
PHP addChild() Function
PHP asXML() Function
PHP attributes() Function
PHP children() Function
PHP SimpleXML count() Function
PHP getDocNamespaces() Function
PHP getName() Function
PHP getNamespaces() Function
PHP registerXPathNamespace() Function
PHP simplexml_import_dom() Function
PHP simplexml_load_file() Function
PHP simplexml_load_string() Function
PHP xpath() Function