Ref/simplexml-Phpdoc专题
simplexml_import_dom
Get a SimpleXMLElement object from a DOM node
说明
SimpleXMLElement<span
class="type">false <span
class="methodname">simplexml_import_dom ( <span
class="methodparam">DOMNode $node [,
string
$class_name =
"SimpleXMLElement" ] )
This function takes a node of a DOM document and makes it into a SimpleXML node. This new object can then be used as a native SimpleXML element.
参数
node
A DOM Element node
class_name
You may use this optional parameter so that <span
class="function">simplexml_import_dom will return an object of
the specified class. That class should extend the <span
class="type">SimpleXMLElement class.
返回值
Returns a SimpleXMLElement 或者在失败时返回
false.
Warning
此函数可能返回布尔值 false,但也可能返回等同于 false
的非布尔值。请阅读
布尔类型章节以获取更多信息。应使用
=== 运算符来测试此函数的返回值。
范例
示例 #1 Importing DOM
<?php
$dom = new DOMDocument;
$dom->loadXML('<books><book><title>blah</title></book></books>');
if (!$dom) {
echo 'Error while parsing the document';
exit;
}
$s = simplexml_import_dom($dom);
echo $s->book[0]->title;
?>
以上例程会输出:
blah
参见
simplexml_load_file
Interprets an XML file into an object
说明
SimpleXMLElement<span
class="type">false <span
class="methodname">simplexml_load_file ( <span
class="methodparam">string $filename
[, string
$class_name =
"SimpleXMLElement" [, <span
class="type">int $options =
0 [, <span
class="type">string $ns =
"" [, <span
class="type">bool $is_prefix =
false ]]]] )
Convert the well-formed XML document in the given file to an object.
参数
filename
Path to the XML file
Note:
Libxml 2 unescapes the URI, so if you want to pass e.g. b&c as the URI parameter a, you have to call simplexml_load_file(rawurlencode('http://example.com/?a=' . urlencode('b&c'))). Since PHP 5.1.0 you don't need to do this because PHP will do it for you.
class_name
You may use this optional parameter so that <span
class="function">simplexml_load_file will return an object of
the specified class. That class should extend the <span
class="type">SimpleXMLElement class.
options
Since PHP 5.1.0 and Libxml 2.6.0, you may also use the options
parameter to specify
additional Libxml parameters.
ns
Namespace prefix or URI.
is_prefix
true if ns is a prefix, false if it's a URI; defaults to
false.
返回值
Returns an object of class <span
class="type">SimpleXMLElement with properties containing the data
held within the XML document, 或者在失败时返回 false.
Warning
此函数可能返回布尔值 false,但也可能返回等同于 false
的非布尔值。请阅读
布尔类型章节以获取更多信息。应使用
=== 运算符来测试此函数的返回值。
错误/异常
Produces an E_WARNING error message for each error found in the
XML data.
小贴士
Use libxml_use_internal_errors to suppress all XML errors, and <span class="function">libxml_get_errors to iterate over them afterwards.
更新日志
| 版本 | 说明 |
|---|---|
| 5.2.0 | The optional parameter is_prefix was added. |
范例
示例 #1 Interpret an XML document
<?php
// The file test.xml contains an XML document with a root element
// and at least an element /[root]/title.
if (file_exists('test.xml')) {
$xml = simplexml_load_file('test.xml');
print_r($xml);
} else {
exit('Failed to open test.xml.');
}
?>
This script will display, on success:
SimpleXMLElement Object
(
[title] => Example Title
...
)
At this point, you can go about using $xml->title and any other elements.
参见
simplexml_load_string
Interprets a string of XML into an object
说明
SimpleXMLElement<span
class="type">false <span
class="methodname">simplexml_load_string ( <span
class="methodparam">string $data [,
string
$class_name =
"SimpleXMLElement" [, <span
class="type">int $options =
0 [, <span
class="type">string $ns =
"" [, <span
class="type">bool $is_prefix =
false ]]]] )
Takes a well-formed XML string and returns it as an object.
参数
data
A well-formed XML string
class_name
You may use this optional parameter so that <span
class="function">simplexml_load_string will return an object of
the specified class. That class should extend the <span
class="type">SimpleXMLElement class.
options
Since PHP 5.1.0 and Libxml 2.6.0, you may also use the options
parameter to specify
additional Libxml parameters.
ns
Namespace prefix or URI.
is_prefix
true if ns is a prefix, false if it's a URI; defaults to
false.
返回值
Returns an object of class <span
class="type">SimpleXMLElement with properties containing the data
held within the xml document, 或者在失败时返回 false.
Warning
此函数可能返回布尔值 false,但也可能返回等同于 false
的非布尔值。请阅读
布尔类型章节以获取更多信息。应使用
=== 运算符来测试此函数的返回值。
错误/异常
Produces an E_WARNING error message for each error found in the
XML data.
小贴士
Use libxml_use_internal_errors to suppress all XML errors, and <span class="function">libxml_get_errors to iterate over them afterwards.
更新日志
| 版本 | 说明 |
|---|---|
| 5.2.0 | The optional parameter is_prefix was added. |
范例
示例 #1 Interpret an XML string
<?php
$string = <<<XML
<?xml version='1.0'?>
<document>
<title>Forty What?</title>
<from>Joe</from>
<to>Jane</to>
<body>
I know that's the answer -- but what's the question?
</body>
</document>
XML;
$xml = simplexml_load_string($string);
print_r($xml);
?>
以上例程会输出:
SimpleXMLElement Object
(
[title] => Forty What?
[from] => Joe
[to] => Jane
[body] =>
I know that's the answer -- but what's the question?
)
At this point, you can go about using $xml->body and such.
参见
目录
- simplexml_import_dom — Get a SimpleXMLElement object from a DOM node
- simplexml_load_file — Interprets an XML file into an object
- simplexml_load_string — Interprets a string of XML into an object