PHP
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

DOM XML> <DOM 関数
Last updated: Fri, 10 Oct 2008

view this page in

dom_import_simplexml

(PHP 5)

dom_import_simplexml SimpleXMLElement オブジェクトから DOMElement オブジェクトを取得する

説明

DOMElement dom_import_simplexml ( SimpleXMLElement $node )

この関数は、SimpleXML クラスの ノード node を受け取り、それを DOMElement ノードに変換します。新しいオブジェクトは、 DOMElement ノードとして使用可能です。

パラメータ

node

SimpleXMLElement ノード。

返り値

追加された DOMElement ノード、あるいは 何らかのエラーが発生した場合は FALSE を返します。

例1 dom_import_simplexml() を用いて SimpleXML を DOM にインポートする

<?php

$sxe 
simplexml_load_string('<books><book><title>blah</title></book></books>');

if (
$sxe === false) {
    echo 
'ドキュメントのパース中にエラー';
    exit;
}

$dom_sxe dom_import_simplexml($sxe);
if (!
$dom_sxe) {
    echo 
'XML の変換中にエラー';
    exit;
}

$dom = new DOMDocument('1.0');
$dom_sxe $dom->importNode($dom_sxetrue);
$dom_sxe $dom->appendChild($dom_sxe);

echo 
$dom->saveXML();

?>


add a note add a note User Contributed Notes
dom_import_simplexml
crescentfreshpot at yahoo dot com
12-Dec-2007 12:22
justinpatrin at php dot net:
> To get a proper DOM document (which you need to do most things) you need...

No you don't. Just do:
<?php
$dom
= dom_import_simplexml($xml)->ownerDocument;
?>
justinpatrin at php dot net
01-Jun-2006 09:44
I've found that newer versions of PHP5 require some special syntax in order to properly convert between SimpleXML and DOM. It's not as easy as calling dom_import_simplexml() with a SimpleXML node. To get a proper DOM document (which you need to do most things) you need:
<?php
//$xml is a SimpleXML instance
$domnode = dom_import_simplexml($xml);
$dom = new DOMDocument();
$domnode = $dom->importNode($domnode, true);
$dom->appendChild($domnode);
?>

Switching back, though, is, well...simple.

<?php
//$dom is a DOMDocument instance
$xml = simplexml_import_dom($dom);
?>

DOM XML> <DOM 関数
Last updated: Fri, 10 Oct 2008
 
 
show source | credits | stats | sitemap | contact | advertising | mirror sites