SimpleXML uses a built-in class called SimpleXMLElement. It converts an XML document into a simpleXMLElement object. This contains an array of all the element, text, and attribute nodes, each of which is, in turn, a SimpleXMLElement object. What makes it so easy to use is that the name of each node becomes a property of the object, giving you direct access to it.
First, you need to load the XML document that you want to process. There are several ways of doing this, depending on whether the XML document is stored as a physical file (either locally or on a remote server) or as a string (e.g., after using the Pos_RemoteConnector).
Loading XML from a file
The easiest way to load an XML document from a file is to use the simplexml_load_file() function. This requires a single argument: a string containing either the path to a local file or the URL of the file on a remote server. You use it like this:
$xml = simplexml_load_file($location);
Alternatively, you can use the SimpleXMLElement class constructor. When used with a file, it requires two extra arguments, in addition to the file’s path or URL, like this:
$xml = new SimpleXMLElement($location, null, true); .
The second argument sets Libxml parameters (see PHP: Predefined Constants - Manual),
which are rarely used. Even though you don’t normally want them, the argument needs to be set to null because the third argument is required when the XML document is stored as a physical file, rather than a string. The third argument is a Boolean value that needs to be set to true.
There’s no advantage to using the SimpleXMLElement constructor unless you need the advanced features offered by the Libxml parameters. Keep things simple by using simplexml_load_file().


LinkBack URL
About LinkBacks
Reply With Quote

LinkBacks Enabled by vBSEO
Bookmarks