PHP4とPHP5のDOM操作: PHP5への移行
PHP4とPHP5では以下のようにDOMのクラスや関数の名前などがだいぶ違う・・・
<?php
$xml = 'sample.xml';
#PHP5のDOM操作
if(PHP_VERSION >= '5'){$dom = new DOMDocument();
$dom->load($xml);
$root = $dom->documentElement;foreach($root->getElementsByTagName("tag1") as $section){
foreach($section->getElementsByTagName("tag2") as $label){var_dump($label->getAttribute("attr1"));
var_dump($label->nodeValue);
}
}
}
#PHP4のDOM操作
else{
$dom = domxml_open_file($xml);
$root = $dom->document_element();foreach($root->get_elements_by_tagname('tag1') as $section){
foreach($section->get_elements_by_tagname('tag2') as $label){var_dump($label->get_attribute('attr1'));
var_dump($label->get_content());
}
}
}
?>
GNU LGPLのdomxml-php4-to-php5を使えばソースコードをほとんど変更せずにPHP4からPHP5に変更できる。
これ -> http://alexandre.alapetite.net/doc-alex/domxml-php4-php5/index.en.html
<?php
if(PHP_VERSION >= '5'){
require_once('domxml-php4-to-php5.php');
}
$xml = 'sample.xml';
$dom = domxml_open_file($xml);
$root = $dom->document_element();foreach($root->get_elements_by_tagname('tag1') as $section){
foreach($section->get_elements_by_tagname('tag2') as $label){var_dump($label->get_attribute('attr1'));
var_dump($label->get_content());
}}
?>