The __construct() function creates a new SimpleXMLElement object.
__construct(data,options,data_is_url,ns,is_prefix);
Parameter | is required | Description |
---|---|---|
data | Required. | A well-formed XML string or the path or URL to an XML document if data_is_url is TRUE |
options | Optional. | Additional Libxml parameters. Is set by specifying the option and 1 or 0 (TRUE or FALSE, e.g. LIBXML_NOBLANKS(1)) |
data_is_url | Optional. | TRUE specifies that data is a path/URL to an XML document instead of string data. Default is FALSE |
ns | Optional. | A namespace prefix or URI |
is_prefix | Optional. | A Boolean value. TRUE if ns is a prefix. FALSE if ns is a URI. Default is FALSE |
Possible values for options:
Returns a SimpleXMLElement object that represents data.
Create a new SimpleXMLElement object, then output the content of the body node:
<?php/*from w w w . ja v a 2s . c o m*/
$note=<<<XML
<?xml version="1.0" encoding="ISO-8859-1"?>
<book>
<name>PHP</name>
<name>Java</name>
</book>
XML;
$xml=new SimpleXMLElement($note);
echo $xml->body;
?>
Assume we have the following XML file, "note.xml":
<?xml version="1.0" encoding="ISO-8859-1"?> <?xml version="1.0" encoding="ISO-8859-1"?> <book> <name>PHP</name> <name>Java</name> </book>
Create a SimpleXMLElement object from a URL:
<?php
$xml=new SimpleXMLElement("note.xml",NULL,TRUE);
echo $xml->asXML();
?>