PHP __construct () function
PHP SimpleXML Reference Manual
Examples
SimpleXMLElement function creates a new object, then the output node of the body:
<?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;
?>
$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;
?>
Running instance »
Definition and Usage
__construct () function creates a new SimpleXMLElement object.
grammar
__construct( data,options,data_is_url,ns,is_prefix );
参数 | 描述 |
---|---|
data | 必需。形式良好的 XML 字符串或 XML 文档的路径或 URL(如果 data_is_url 是 TRUE)。 |
options | 可选。规定附加的 Libxml 参数。通过指定选项为 1 或 0(TRUE 或 FALSE,例如 LIBXML_NOBLANKS(1))进行设置。 可能的值:
|
data_is_url | 可选。如果是 TRUE 表明 data 是 XML 文档的路径或 URL,而不是字符串数据。默认是 FALSE。 |
ns | 可选。规定命名空间前缀或 URI。 |
is_prefix | 可选。规定一个布尔值。如果 ns 是前缀则为 TRUE,如果 ns 是 URI 则为 FALSE。默认是 FALSE。 |
technical details
return value: | Returns data of SimpleXMLElement object representation. |
---|---|
PHP version: | 5.0.1+ |
PHP Update Log: | PHP 5.1.2: Added data_is_url options and parameters. PHP 5.2: Added ns and is_prefix parameters. |
More examples
Suppose we have the following XML document, " note.xml ":
<?xml version="1.0" encoding="ISO-8859-1"?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
<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 the URL:
<?php
$xml=new SimpleXMLElement("note.xml",NULL,TRUE);
echo $xml->asXML();
?>
$xml=new SimpleXMLElement("note.xml",NULL,TRUE);
echo $xml->asXML();
?>
Running instance »
PHP SimpleXML Reference Manual