PHP __construct()Functions

PHP SimpleXML 参考手册PHP SimpleXML Reference Manual

Examples

The function creates a new SimpleXMLElement object, then outputs the content of the body node:

<?php $note=<<<XML <note> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body> </note> XML; $xml=new SimpleXMLElement($note); echo $xml->body; ?>

Run example »

Definition and Usage

The __construct() function creates a new SimpleXMLElement object.


Syntax

__construct(data,options,data_is_url,ns,is_prefix);

Parameters Description
data Required. A well-formed XML string or the path or URL of an XML document (if data_is_url is TRUE).
options Optional. Specifies additional Libxml parameters. Set by specifying options as 1 or 0 (TRUE or FALSE, e.g., LIBXML_NOBLANKS(1)).

Possible values:

  • LIBXML_COMPACT - Activates the optimized configuration of nodes (can speed up applications)
  • LIBXML_DTDATTR - Sets default DTD attributes
  • LIBXML_DTDLOAD - Loads an additional subset
  • LIBXML_DTDVALID - Validates DTD validity
  • LIBXML_NOBLANKS - Removes empty nodes
  • LIBXML_NOCDATA - Sets CDATA as text nodes
  • LIBXML_NOEMPTYTAG - Expands empty tags (e.g., <br/> to <br></br>), only valid in the DOMDocument->save() and DOMDocument->saveXML() functions
  • LIBXML_NOENT - Substitutes entities
  • LIBXML_NOERROR - Does not display error reports
  • LIBXML_NONET - Stops accessing the network when loading documents
  • LIBXML_NOWARNING - Does not display warning reports
  • LIBXML_NOXMLDECL - Drops the XML declaration when storing a document
  • LIBXML_NSCLEAN - Removes redundant namespace declarations
  • LIBXML_PARSEHUGE - Sets the XML_PARSE_HUGE flag to relax any enforced limits of the parser. This affects things such as maximum document depth and text node size limits.
  • LIBXML_XINCLUDE - Uses XInclude substitution
  • LIBXML_ERR_ERROR - Gets recoverable errors
  • LIBXML_ERR_FATAL - Gets fatal errors
  • LIBXML_ERR_NONE - Gets no errors
  • LIBXML_ERR_WARNING - Gets simple warnings
  • LIBXML_VERSION - Gets the libxml version (e.g., 20605 or 20617)
  • LIBXML_DOTTED_VERSION - Gets the dotted libxml version (e.g., 2.6.5 or 2.6.17)
data_is_url Optional. If TRUE, it indicates datais the path or URL of the XML document, not string data. Default is FALSE.
ns Optional. Specifies the namespace prefix or URI.
is_prefix Optional. Specifies a boolean value. Ifnsis a prefix then TRUE, ifnsis a URI then FALSE. Default is FALSE.

Technical Details

Return Values: Returns adataSimpleXMLElement object representing the ...
PHP Version: 5.0.1+
PHP Changelog: PHP 5.1.2: Added theoptionsanddata_is_urlparameter.
PHP 5.2: Addednsandis_prefixparameter.


More Examples

Assume we have the following XML filenote.xml :

<?xml version="1.0" encoding="UTF-8"?>
<note>
    <to>Tove</to>
    <from>Jani</from>
    <heading>Reminder</heading>
    <body>Don't forget me this weekend!</body>
</note>

Example 1

Create a SimpleXMLElement object from a URL:

<?php $xml=new SimpleXMLElement("note.xml",0,TRUE); echo $xml->asXML(); ?>

Run example »


PHP SimpleXML 参考手册PHP SimpleXML Reference Manual

Other Extensions