PHP __construct()関数
例
文字列からSimpleXMLElementオブジェクトを作成します。
<?php
$note=<<<XML
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Do not forget me this weekend!</body>
</note>
XML;
$xml=new SimpleXMLElement($note);
echo $xml->asXML();
?>
定義と使用法
__construct()関数は、新しいSimpleXMLElementオブジェクトを作成します。
構文
SimpleXMLElement::__construct(data, options, data_is_url, ns, is_prefix)
パラメータ値
Parameter | Description |
---|---|
data | Required. Specifies A well-formed XML string or the path or URL to an XML document if data_is_url is TRUE |
options | Optional. Specifies additional Libxml parameters. Is set by specifying the option and 1 or 0 (TRUE or FALSE, e.g. LIBXML_NOBLANKS(1)) Possible values:
|
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. Specifies a namespace prefix or URI |
is_prefix | Optional. Specifies a Boolean value. TRUE if ns is a prefix. FALSE if ns is a URI. Default is FALSE |
技術的な詳細
戻り値: | データを表すSimpleXMLElementオブジェクトを返します |
---|---|
PHPバージョン: | 5.0以降 |
PHP変更ログ: | PHP 5.2.0:オプションのnsおよびis_prefixパラメーターを追加しました。 PHP 5.1.2:オプションのオプションとdata_is_urlパラメーターを追加しました。 |
その他の例
次のXMLファイル「note.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>
例
URLからSimpleXMLElementオブジェクトを作成します。
<?php
$xml=new SimpleXMLElement("note.xml", 0, TRUE);
echo $xml->asXML();
?>
❮PHPSimpleXMLリファレンス