SimpleXML :
• SimpleXML is an extension that allows us to easily manipulate and get XMLdata.
• The SimpleXML extension is the tool of choice for parsing an XMLdocument.
• SimpleXML turns an XML document into a data structure you can iterate through likea
collection of arrays andobjects.
• The SimpleXML extension includes interoperability with the DOM for writing XML filesand
built-in XPathsupport.
• SimpleXML is easier to code than the DOM, as its nameimplies.
SimpleXMLElement class represents an element in an XML document.
• To create root element of xml document, first create object of SimpleXMLElement class and
initialize with rootelement.
• For example :
• $bk=newSimpleXMLElement(“<bookstore/>”);
Methods or functions of simpleXMLElement class
Function name description syntax example
addChild() The addChild() addChild(name,val $book = $bk->addchild(“book”);
function adds a ue);
child element to
the SimpleXML
elemen
addAttribute() adds an attribute addAttribute(name, $book->addAttribute(“Category” ,
to the value); “Technical”);
SimpleXML
element.
getName() Returns the name getName(); $bk->getName();
of the XML tag
referenced by the
SimpleXML
element
asXML() Returns a well- asXML([filename])
formed XML string ; echo $bk->asXML();
(XML version 1.0)
from a SimpleXML
object
children() Returns the children() foreach ($book->children() as $child)
children of a {
specified node as echo "Child node: " . $child .
an array "<br>";
}
attributes(); Returns the attributes(); foreach ($book->attributes () as
attributes/values $k=>$v)
of an element {
echo $k : $v . "<br>";
}
count(); The count() count(); $cnt=$book->count();
function counts
the children of a
specified node.
17