PHP SimpleXMLElement Create
Description
The __construct() function creates a new SimpleXMLElement object.
Syntax
__construct(data,options,data_is_url,ns,is_prefix);
Parameter
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:
- LIBXML_COMPACT - Activate nodes allocation optimization (may speed up application)
- LIBXML_DTDATTR - Set default DTD attributes
- LIBXML_DTDLOAD - Load external subset
- LIBXML_DTDVALID - Validate with the DTD
- LIBXML_NOBLANKS - Remove blank nodes
- LIBXML_NOCDATA - Merge CDATA as text nodes
- LIBXML_NOEMPTYTAG - Expand empty tags (e.g. <br/> to <br></br>), only available in the DOMDocument->save() and DOMDocument->saveXML() functions
- LIBXML_NOENT - Substitute entities
- LIBXML_NOERROR - Do not show error reports
- LIBXML_NONET - Disable network access while loading documents
- LIBXML_NOWARNING - Do not show warning reports
- LIBXML_NOXMLDECL - Drop the XML declaration when saving a document
- LIBXML_NSCLEAN - Remove redundant namespace declarations
- LIBXML_PARSEHUGE - Sets XML_PARSE_HUGE flag, which relaxes any hardcoded limit from the parser. This affects limits like maximum depth of a document and limits of the size of text nodes
- LIBXML_XINCLUDE - Implement XInclude substitution
- LIBXML_ERR_ERROR - Get recoverable errors
- LIBXML_ERR_FATAL - Get fatal errors
- LIBXML_ERR_NONE - Get no errors
- LIBXML_ERR_WARNING - Get simple warnings
- LIBXML_VERSION - Get libxml version
- LIBXML_DOTTED_VERSION - Get dotted libxml version
Return
Returns a SimpleXMLElement object that represents data.
Example
Create a new SimpleXMLElement object, then output the content of the body node:
<?php/* w ww . java2 s . c om*/
$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;
?>
Example 2
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>//from w w w. ja v a 2s .co m
<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();
?>