SHOEISHA iD

※旧SEメンバーシップ会員の方は、同じ登録情報(メールアドレス&パスワード)でログインいただけます

DeveloperZine(デベロッパージン)- エンジニアの意思決定を支える技術情報メディア ProductZine

CodeZine編集部では、現場で活躍するデベロッパーをスターにするためのカンファレンス「Developers Summit」や、エンジニアの生きざまをブーストするためのイベント「Developers Boost」など、さまざまなカンファレンスを企画・運営しています。

japan.internet.com翻訳記事

PHPでDOMを操作する

PHP 5のDOMエクステンションで、DOMツリーをゼロから作成

ノードの等値性をチェックする

 2つのノードが同じかチェックするには関数isSameNodeを使います。

isSameNodeメソッド
bool DOMNode::isSameNode(DOMNode $node)

 この関数は、ノードが等しいときは論理値のTRUEを返し、等しくないときはFALSEを返します。引数$nodeは現在のノードと比較するノードです。この比較はノードのコンテンツに基づいて行われるものではないことに注意してください。

//Checking if two nodes are equals
$author1 = $root->getElementsByTagName('autor')->item(0);
$author2 = $root->getElementsByTagName('autor')->item(1);
//The verifyNodes function call
verifyNodes($author1,$author2);
function verifyNodes($currentNode, $node)
{
   if (($currentNode->isSameNode($node))==true)
   {
      echo "These two nodes are the same";
   }   
}

新しいツリーを作成する

 既存のツリーを最初から使わなくてもかまいません。PHP 5のDOMエクステンションではツリーをゼロから作ることもできます。次の例では、まったく新しいXML文書を作成します。ここではコメントノードを作成する関数とCDATAノードを作成する関数を使用しています。

createCommentメソッド
DOMComment DOMDocument::createComment(string $data)

 新しいコメントノードを作成します。引数$dataはノードのコンテンツです。

createCDATASectionメソッド
DOMCDATASection DOMDocument::createCDATASection(string $data)

 新しいCDATAノードを作成します。引数$dataはノードのコンテンツです。

 リスト2の例ではオブジェクトツリーを作成し、Flowers.xmlという名前で保存します。

リスト2 新しいDOMツリーの作成
<?php 
  
  //Create a document instance 
  $document = new DOMDocument();
  
  //Formats output with indentation
  $document->formatOutput = true;
  
  //Create a comment
  $comment = $document->createComment('Beautiful flowers!!!');
  $document->appendChild( $comment ); 

  //Create the <flowers> root element 
  $root = $document->createElement( 'flowers' );
  $document->appendChild( $root );
  
  //Create the <tulips> children of the root
  $tulips = $document->createElement( 'tulips' );
  
  //Create the first child of the <tulips> element,<bulbs>, 
  // and set its attribute
  $bulbs_1 = $document->createElement( 'bulbs' );   
  $bulbs_1->setAttribute('price','€ 7.65');
  $bulbs_1->appendChild($document->createTextNode( 'Parrot'));
  $tulips->appendChild( $bulbs_1 );
  
  //Create the second child of the <tulips> element,<bulbs>, 
  // and set its attribute
  $bulbs_2 = $document->createElement( 'bulbs' );    
  $bulbs_2->setAttribute('color','magenta');
  $bulbs_2->appendChild($document->createTextNode( 'Lily flowering' ));
  $tulips->appendChild( $bulbs_2 );
  
  //Append the <tulips> node to the root
  $root->appendChild( $tulips );
  
  //Create a CDATA section
  $cdata = $document->createCDATASection(
    '<gladiolus><species>Sword Lily</species>'.
    '<species>Starface</species></gladiolus>');
  $document->appendChild( $cdata ); 
  
  //Save the object tree to Flowers.xml 
  echo $document->saveXML();
  $document->save('Flowers.xml');
?>

 新しい文書Flower.xmlは次のようになります。

<?xml version="1.0" encoding="ISO-8859-1"?>
<!--Beautiful flowers!!!-->
<flowers>
  <tulips>
    <bulbs price="€ 7.65">Parrot</bulbs>
    <bulbs color="magenta">Lily flowering</bulbs>
  </tulips>
</flowers>
<![CDATA[<gladiolus>
    <species>Sword Lily</species>
    <species>Starface</species>
  </gladiolus>
]]>

 以上、PHP 5のDOMエクステンションを簡単に紹介しました。これらの情報は既存のXML(HTML)文書を操作したり、文書をゼロから作成したりするときに役立つはずです。

この記事は参考になりましたか?

連載通知を行うには会員登録(無料)が必要です。
既に会員の方はを行ってください。
japan.internet.com翻訳記事連載記事一覧

もっと読む

この記事の著者

japan.internet.com(ジャパンインターネットコム)

japan.internet.com は、1999年9月にオープンした、日本初のネットビジネス専門ニュースサイト。月間2億以上のページビューを誇る米国 Jupitermedia Corporation (Nasdaq: JUPM) のニュースサイト internet.comEarthWeb.com からの最新記事を日本語に翻訳して掲載するとともに、日本独自のネットビジネス関連記事やレポートを配信。

※プロフィールは、執筆時点、または直近の記事の寄稿時点での内容です

Octavia Andreea Anghel(Octavia Andreea Anghel)

経験豊富なPHP開発者。現在は、国内外のソフトウェア開発コンテストに参加するプログラミングチームの主任トレーナーを務める。国レベルの教育プロジェクト開発のコンサルティングも担当している。共著書に『XML technologies?XML in Java』があり、XML部分の執筆を担当。PHPやXML...

※プロフィールは、執筆時点、または直近の記事の寄稿時点での内容です

この記事は参考になりましたか?

この記事をシェア

CodeZine(コードジン)
https://codezine.jp/article/detail/4278 2009/08/27 14:00

イベント

CodeZine編集部では、現場で活躍するデベロッパーをスターにするためのカンファレンス「Developers Summit」や、エンジニアの生きざまをブーストするためのイベント「Developers Boost」など、さまざまなカンファレンスを企画・運営しています。

新規会員登録無料のご案内

  • ・全ての過去記事が閲覧できます
  • ・会員限定メルマガを受信できます

メールバックナンバー