A number of SimpleXML functions are available for loading and parsing the XML document. These functions are introduced in this section, along with several accompanying examples. Note To take advantage of SimpleXML when using PHP versions older than 6.0, you need to disable the PHP directivezend.ze1_compatibility_mode. Loading XML from a File Thesimplexml_load_file()function loads an XML file into an object. Its prototype follows: object simplexml_load_file(string filename [, string class_name]) If a problem is encountered loading the file,FALSEis returned. If the optionalclass_nameparameter is included, an object of that class will be returned. Of course,class_nameshould extend theSimpleXMLElementclass. Consider an example:
<?php This code returns the following: object(SimpleXMLElement)#1 (1) { Note that dumping the XML will not cause the attributes to show. To view attributes, you need to use theattributes()method, introduced later in this section. Loading XML from a String If the XML document is stored in a variable, you can use thesimplexml_load_string()function to read it into the object. Its prototype follows: object simplexml_load_string(string data) This function is identical in purpose tosimplexml_load_file(), except that the lone input parameter is expected in the form of a string rather than a file name. Loading XML from a DOM The Document Object Model (DOM) is a W3C specification that offers a standardized API for creating an XML document, and subsequently navigating, adding, modifying, and deleting its elements. PHP provides an extension capable of managing XML documents using this standard, titled the DOM XML extension. You can use thesimplexml_import_dom()function to convert a node of a DOM document into a SimpleXML node, subsequently exploiting use of the SimpleXML functions to manipulate that node. Its prototype follows: object simplexml_import_dom(domNode node)
blog comments powered by Disqus |
|
|
|
|
|
|
|