SimpleXML is an easy-to-use XML toolset provided after PHP5 that transforms XML into an easy-to-handle object or organizes XML data to be generated. However, it does not apply to XML that contains namespace, and is guaranteed to be complete in XML format (well-formed). It provides three methods: Simplexml_import_dom, Simplexml_load_file, simplexml_load_string, and the function name is very intuitive to illustrate the function. All three functions return the SimpleXMLElement object, and the read/add of the data is done through the simplexmlelement operation.
$string = <<<xml<?xml version= ' 1.0 '?><document> <cmd>login</cmd> < login>imdonkey</login></document>xml; $xml = simplexml_load_string ($string);p rint_r ($xml); $login = $ Xml->login;//returned here is still a SimpleXMLElement object Print_r ($login); $login = (string) $xml->login;//when doing a data comparison, Note that the Print_r ($login) must be cast first;
The advantage of SimpleXML is that it is easy to develop, and the downside is that it will load the entire XML into memory before processing it, so it may be too weak to parse an XML document that is hyper-content. If you are reading small files, and the XML does not contain namespace, then SimpleXML is a good choice.