# dom_import_simplexml

(PHP 5, PHP 7, PHP 8)

dom_import_simplexml — Gets a DOMAttr or DOMElement object from a SimpleXMLElement object

### 说明

dom_import_simplexml(object `$node`): DOMAttr|DOMElement

This function takes the given attribute or element `node` (a SimpleXMLElement instance) and creates a DOMAttr or DOMElement node, repectively. The new DOMNode refers to the same underlying XML node as the SimpleXMLElement.

### 参数

`node`

  The attribute or element node to import (a SimpleXMLElement instance).

### 返回值

The DOMAttr or DOMElement.

### 更新日志

版本 | 说明
--- | ---
8.0.0 | This function no longer returns `null` on failure.

### 示例

示例 #1 Import SimpleXML into DOM with dom_import_simplexml()

`<?php
$sxe = simplexml_load_string('<books><book><title>blah</title></book></books>');
if ($sxe === false) {
echo 'Error while parsing the document';
exit;
}
$dom_sxe = dom_import_simplexml($sxe);
if (!$dom_sxe) {
echo 'Error while converting XML';
exit;
}
$dom = new DOMDocument('1.0');
$dom_sxe = $dom->importNode($dom_sxe, true);
$dom_sxe = $dom->appendChild($dom_sxe);
echo $dom->saveXML();
?>`

以上示例会输出：

    <?xml version="1.0"?>
    <books><book><title>blah</title></book></books>

示例 #2 Import SimpleXML into DOM and modify SimpleXML through DOM

Error handling omitted for brevity.

`<?php
$sxe = simplexml_load_string('<books><book><title>blah</title></book></books>');
$elt = dom_import_simplexml($sxe);
$elt->setAttribute("foo", "bar");
echo $sxe->asXML();
?>`

以上示例会输出：

    <?xml version="1.0"?>
    <books foo="bar"><book><title>blah</title></book></books>

### 参见

- simplexml_import_dom() - Get a SimpleXMLElement object from an XML or HTML node

`↑` and `↓` to navigate • `Enter` to select • `Esc` to close • `/` to open