simplexml_import_dom

Get a SimpleXMLElement object from a DOM node

说明

SimpleXMLElement simplexml_import_dom ( 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 simplexml_import_dom will return an object of the specified class. That class should extend the 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

参见

  • dom_import_simplexml

simplexml_load_file

Interprets an XML file into an object

说明

SimpleXMLElement simplexml_load_file ( string $filename [, string $class_name = "SimpleXMLElement" [, int $options = 0 [, string $ns = "" [, 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 simplexml_load_file will return an object of the specified class. That class should extend the 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 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 libxml_get_errors to iterate over them afterwards.

范例

示例 #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
  • SimpleXMLElement::__construct
  • libxml_use_internal_errors

simplexml_load_string

Interprets a string of XML into an object

说明

SimpleXMLElement simplexml_load_string ( string $data [, string $class_name = "SimpleXMLElement" [, int $options = 0 [, string $ns = "" [, 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 simplexml_load_string will return an object of the specified class. That class should extend the 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 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 libxml_get_errors to iterate over them afterwards.

范例

示例 #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_load_file
  • SimpleXMLElement::__construct
  • libxml_use_internal_errors

目录