浅谈如何利用PHP访问和操作DOM

原创
开发 后端
文档对象模型(Document Object Model,DOM)是一个W3C标准,它基于一套用于将XML或HTML文档表现为一个树对象的接口。本文将为大家介绍如何使用PHP访问和操作DOM。

DOMÊ÷¶¨ÒåÁËÎĵµµÄÂß¼­½á¹¹£¬ÒÔ¼°¿ØÖÆÄã·ÃÎʺͲÙ×÷ÕâЩÎĵµµÄ·½·¨¡£Ê¹ÓÃDOM£¬¿ª·¢ÈËÔ±¿ÉÒÔ´´½¨XML»òHTMLÎĵµ£¬²Ù×÷ËüÃǵĽá¹û£¬Ôö¼Ó¡¢Ð޸ĺÍɾ³ýÎĵµ ÔªËؼ°ÄÚÈÝ¡£¿ÉÒÔ´ÓÈκαà³ÌÓïÑÔ·ÃÎÊDOM£¬±¾ÎÄʹÓÃPHP 5 DOMÀ©Õ¹£¬ËüÊÇPHPºËÐĵÄÒ»²¿·Ö£¬Òò´Ë³ýÁËPHPÍ⣬²»ÐèÒª°²×°ÆäËüÈí¼þ¡£

DOMÊ÷½Úµã×ñÑ­XMLÃüÃû¹æ·¶£¬È磺

1¡¢Document½Úµã -- ±íʾDOMDocument½Ó¿Ú

2¡¢Element½Úµã -- ±íʾDOMElement½Ó¿Ú

3¡¢Attribute½Úµã -- ±íʾDOMAttr½Ó¿Ú

4¡¢Comment½Úµã -- ±íʾDOMComment½Ó¿Ú

5¡¢Text½Úµã -- ±íʾDOMText½Ó¿Ú

ÌáÈ¡ÔªËØ

ÕâһС½Ú½éÉÜÈçºÎ´ÓDOMÊ÷ÖÐÌáÈ¡ÔªËغÍÖµ£¬±¾ÎÄʹÓÃBook.xml×÷ΪÀý×Ó½øÐÐ˵Ã÷£¬ÆäÄÚÈÝÈçÇåµ¥1Ëùʾ¡£

Çåµ¥1 Book.xml

  1. <?xml version="1.0" encoding="UTF-8" standalone="yes" ?> 
  2. <book> 
  3. <!--XML Processing [part I] --> 
  4.  <name>XML Processing I</name> 
  5.   <author>John Smith Jr.</author> 
  6.   <publisher>HisOwnTM</publisher> 
  7.   <ISBN>111-222-333-4441</ISBN> 
  8.   <contents> 
  9.     <chapter_I> 
  10.       <title>What is XML about ?</title> 
  11.       <content>XML (Extensible Markup Language) is a ...</content> 
  12.     </chapter_I> 
  13.     <chapter_II> 
  14.       <title>SAX</title> 
  15.       <content>SAX is a simple API for ...</content> 
  16.    </chapter_II> 
  17.     <chapter_III> 
  18.       <title>StAX</title> 
  19.       <content>Much powerful and flexible, StAX, is very...</content> 
  20.     </chapter_III> 
  21.     <chapter_IV> 
  22.       <title>DOM 
  23.         <subtitle>DOM concept  
  24.           <continut>Starting to use DOM...</continut> 
  25.         </subtitle> 
  26.         <subchapter_IV_I> 
  27.          <title>First DOM application...</title> 
  28.           <content>Here it is your first DOM application...</content> 
  29.         </subchapter_IV_I>  
  30.       </title>              
  31.     </chapter_IV>           
  32.     <end>The end...</end>           
  33.   </contents> 
  34. <!-- See you in XML Processing [part II] --> 
  35. </book> 

ÏÈÏÂÔر¾ÎÄʹÓõÄPHP´úÂëѹËõ°ü£¬http://assets.devx.com/sourcecode/41975_oa_mainsource.zip£¬½«Book.xmlºÍѹ Ëõ°ü½âѹºó·ÅÔÚͬһ¸öĿ¼Ï¡£

µÚÒ»¸öʾÀýÓ¦ÓóÌÐòʹÓÃBook.xmlÎĵµ£¬ÌáÈ¡³ö¹ØÁªµÄÊ÷£¬È»ºóʹÓÃDOMElement½Ó¿ÚµÄgetElementsByTagName·½·¨ÏÔʾµÚÒ»¸ö×Ó½ÚµãʵÀý£º

DOMNodeList DOMElement::getElementsByTagName(string $name)£ºÕâ¸ö·½·¨·µ»ØËùÓÐ$name²ÎÊýÖ¸¶¨µÄ±êÇ©ÃûµÄ×ÓÔªËØ¡£ÏÂÃæµÄÀý×Ó²éÕÒ<book>¸ù½Úµã £¬È»ºó²éÕÒËüµÄ×Ó½Úµã <author>£¬<publisher>ºÍ <name>ÔªËØ£¬Ñ¡Ôñÿ¸ö×Ó½ÚµãµÄµÚÒ»¸ö£¬×îºó´òÓ¡ÕâЩ½ÚµãµÄÖµ£º

  1. <?php 
  2.  // ´´½¨Ò»¸öÎĵµÊµÀý  
  3.   $doc = new DOMDocument();  
  4.   //ÔØÈëBook.xmlÎļþ  
  5.   $doc->load( 'Book.xml' );  
  6.   //ʹÓÃbook±êÇ©ÃûËÑË÷ËùÓÐÔªËØ  
  7.   $books = $doc->getElementsByTagName( "book" );  
  8.   //ʹÓÃauthor±êÇ©ÃûËÑË÷ËùÓÐÔªËØ  
  9.   $authors = $doc->getElementsByTagName( "author" );  
  10.   //·µ»ØµÚÒ»¸ö±êÇ©ÃûΪauthorµÄÔªËØ  
  11.   $author = $authors->item(0)->nodeValue;  
  12.   //ÒÔpublisher±êÇ©ÃûËÑË÷ËùÓÐÔªËØ 
  13.   $publishers = $doc->getElementsByTagName( "publisher" );  
  14.   //·µ»ØµÚÒ»¸öÕÒµ½µÄ±êÇ©ÃûΪpublisherµÄÔªËØ  
  15.    $publisher = $publishers->item(0)->nodeValue;  
  16.   //ËÑË÷±êÇ©ÃûΪnameµÄËùÓÐÔªËØ 
  17.   $titles = $doc->getElementsByTagName( "name" );  
  18.   //·µ»Ø±êÇ©ÃûΪnameµÄµÚÒ»¸öÕÒµ½µÄÔªËØ  
  19.   $title = $titles->item(0)->nodeValue;  
  20.   //´òÓ¡ÕÒµ½µÄÖµ  
  21.   echo "$title - $author - $publisher \n";  
  22.  ?> 

×îºóÒ»ÐÐÊÇ´òÓ¡µÚÒ»¸ö±êÌ⣬µÚÒ»¸ö×÷Õߣ¬µÚÒ»¸ö³ö°æÉÌ£¬Ê¹ÓÃÁ¬×Ö·û·Ö¸ô£¬Êä³ö£º

XML Processing I - John Smith Jr. - HisOwnTM

µÝ¹éä¯ÀÀDOMÊ÷

ÒòΪXMLÎĵµ½á¹¹ÖÐÒ»¸ö±êÇ©¿ÉÒÔ°üÀ¨ÁíÒ»¸ö±êÇ©£¨·ÖÖ§Ê÷£©£¬Ê£Ï¾ÍÊÇÒ¶×ӽڵ㣬Òò´ËÄã¿ÉÒÔä¯ÀÀÍêÕûµÄÊ÷»ò´ÓÈκνڵ㿪ʼµÝ¹éä¯ÀÀ×ÓÊ÷ ¡£ÏÂÃæµÄÀý×ÓÊÇ´ÓÈκοªÊ¼½Úµã£¨$node£©ä¯ÀÀÏÂÃæµÄXML×ÓÊ÷£¬²¢Áгö½ÚµãµÄÃû×ÖºÍÖµ¡£

  1. function getNodesInfo($node)  
  2. {  
  3.    if ($node->hasChildNodes())  
  4.    {  
  5.       $subNodes = $node->childNodes; 
  6.       foreach ($subNodes as $subNode)  
  7.       { 
  8.          if (($subNode->nodeType != 3) ||   
  9.             (($subNode->nodeType == 3) &&  
  10.             (strlen(trim($subNode->wholeText))>=1)))     
  11.          {  
  12.             echo "Node name: ".$subNode->nodeName."\n";  
  13.             echo "Node value: ".$subNode->nodeValue."\n";  
  14.          }  
  15.          getNodesInfo($subNode);           
  16.       }  
  17.    }         
  18. }    

ÉÏÃæµÄÀý×ÓʹÓÃÏÂÃæµÄÌõ¼þÈ¥³ýÁËËùÓпÕÎı¾½Úµã£¬ÈÃÊä³ö¿´ÆðÀ´¸ü¸É¾»£º

  1. if (($subNode->nodeType != 3) ||   
  2.    (($subNode->nodeType == 3) &&  
  3.    (strlen(trim($subNode->wholeText))>=1)))    

Ç°ÃæµÄ´úÂë¼ì²é½ÚµãÊÇ·ñ±»´¦Àí£¬Í¬Ñù£¬Äã¿ÉÒÔÉèÖÃÔ¤¶¨ÒåµÄpreserveWhiteSpaceÊôÐÔ£¬ËüÒƳýÈßÓàµÄ¿Õ°×£¬Ä¬ÈÏÖµÊÇTRUE¡£

ΪÁ˲âÊÔÕâ¸ö¹¦ÄÜ£¬ÏÂÃæÕâ¸öÀý×Ó´«µÝBook.xmlÎĵµµÄ¸ù½Úµã¸øµÝ¹éº¯ÊýgetNodesInfo£¬È»ºó´òÓ¡³öÕû¸öDOMÊ÷µÄ±êÇ©ºÍÖµ£º

  1. <?php 
  2. //´´½¨Ò»¸öÎĵµÊµÀý   
  3. $doc = new DOMDocument();  
  4. //ÔØÈëBook.xmlÎļþ  
  5. $doc->load( 'Book.xml' );   
  6. //ÉèÖöÔÏóÊ÷¸ù   
  7. $root = $dom->firstChild;  
  8. // µÝ¹éº¯ÊýÁгö×ÓÊ÷µÄËùÓÐ½Úµã  
  9. function getNodesInfo($node)  
  10. {  
  11.    if ($node->hasChildNodes())  
  12.    {  
  13.       $subNodes = $node->childNodes;  
  14.      foreach ($subNodes as $subNode)  
  15.       {  
  16.          if (($subNode->nodeType != 3) ||   
  17.             (($subNode->nodeType == 3)    
  18.             &&(strlen(trim($subNode->wholeText))>=1)))     
  19.          {  
  20.          echo "Node name: ".$subNode->nodeName."\n";  
  21.          echo "Node value: ".$subNode->nodeValue."\n";  
  22.      }  
  23.       getNodesInfo($subNode);           
  24.       }  
  25.    }        
  26. }     
  27. //µ÷ÓÃgetNodesInfoº¯Êý  
  28. getNodesInfo($root);  
  29. ?> 

ÏÔʾÊä³öµÄС²¿·ÖÄÚÈÝ

 Í¼1ÏÔʾÁËÊä³öµÄС²¿·ÖÄÚÈÝ

ͼ- 1ÎĵµÄÚÈÝ£ºÕâ¸öͼÏÔʾÁËͨ¹ýgetNodesInfoµÝ¹éº¯ÊýÔËÐÐBook.xmlµÄ²¿·ÖÊä³öÄÚÈÝ

#p#

Ôö¼Óнڵã

DOMNode½Ó¿Ú°üÀ¨¶à¸ö´´½¨Ð½ڵãºÍÔÚDOMÊ÷ÖвåÈë½ÚµãµÄ·½·¨£¬Èç¹ûÒª´´½¨Ò»¸öнڵ㣬¿ÉÒÔʹÓÃcreateElement»òcreateTextNode·½·¨£¬È»ºó £¬ÎªÁËÔö¼ÓÒ»¸öнڵ㵽DOMÊ÷ÉÏ£¬¿ÉÒÔµ÷ÓÃappendChild»òinsertBefore·½·¨£¬appendChild·½·¨Ôö¼ÓÒ»¸öеÄ×ӽڵ㵽Ìض¨½ÚµãµÄ×Ó½ÚµãÁбíµÄºóÃ棬¶ø insertBefore·½·¨ÊÇÔÚÌض¨½ÚµãµÄÇ°Ãæ²åÈëÒ»¸ö½Úµã¡£

ÏÂÃæÊÇÕâЩ·½·¨µÄÔ­ÐÍ£º

1¡¢DOMElement createElement(string $name [, string $value ]) £ºÕâ¸ö·½·¨´´½¨ÁËÒ»¸öDOMElementÀàµÄʵÀý£¬$name²ÎÊý±íʾÐÂÔªËصıêÇ©Ãû£¬$value²ÎÊý ±íʾԪËصÄÖµ£¬ÄãÒ²¿ÉÒÔÉÔºóʹÓÃDOMElement->nodeValueÊôÐÔÆäÖµ¡£

2¡¢DOMText createTextNode(string $content)£ºÕâ¸ö·½·¨´´½¨ÁËÒ»¸öDOMTextÀàµÄʵÀý£¬$content²ÎÊý±íʾеÄÎı¾½ÚµãµÄÎı¾ÄÚÈÝ¡£

3¡¢DOMNode DOMNode::appendChild(DOMNode $newnode)£ºÕâ¸öº¯ÊýÀ©Õ¹ÁËÏÖÓÐ×Ó½Úµãĩβ$newnode²ÎÊý£¬»ò´´½¨Ò»¸öеİüÀ¨Ö¸¶¨½ÚµãµÄ×Ó½ÚµãÁÐ±í¡£

4¡¢DOMNode DOMNode::insertBefore(DOMNode $newnode [,DOMNode $refnode])£ºÕâ¸ö·½·¨ÔÚ$refnode½ÚµãÇ°²åÈë$newnode²ÎÊý£¬Èç¹û$refnode½Úµã¶ªÊ§£¬Ð嵀 ½Úµã¾ÍÌí¼Óµ½½ÚµãµÄ×Ó½ÚµãÁбíÇ°¡£

ÏÂÃæµÄÀý×Ó´´½¨ÁËÒ»¸ö<bibliography>½Úµã£¬²¢½«Æä×·¼Óµ½½ÚµãµÄĩβ£º

  1. //´´½¨Ò»¸öÐÂÔªËØ   
  2. $newElement = $dom->createElement('bibliography','Martin Didier, Professional XML');  
  3. //ʹÓÃappendChildº¯Êý½«Æä×·¼Óµ½¸ù½Úµã  
  4. //µ÷ÓÃappendChildº¯Êý  
  5. appendNewChild($root,$newElement);  
  6. //Õâ¸öº¯Êý×·¼ÓÁËÒ»¸öеÄ×Ó½Úµã  
  7. function appendNewChild($currentNode, $node)  
  8. {  
  9.    $currentNode->appendChild($node);  

getNodeInfo()º¯ÊýÔËÐнá¹û

Èç¹ûÄãͨ¹ýgetNodeInfo()º¯ÊýÔËÐеóö½á¹û£¬Ä㽫»á¿´µ½Èçͼ2ËùʾµÄÊä³ö¡£

ͼ- 2 ×·¼ÓµÄ½Úµã£ºÕâ¸öͼÏÔʾÁËмӵÄ<bibliography>½ÚµãºÍËüµÄÄÚÈÝ

ÏÂÃæµÄÀý×ÓÊÇÔÚ<publisher>½ÚµãÔö¼ÓÒ»¸ö<foreword>×ӽڵ㣺

  1. //´´½¨Ò»¸öеÄ<foreword>ÔªËØ  
  2. $newElement = $dom->createElement('foreword',  
  3.    'What I love about this book is that it '.  
  4.    'grew out of just such a process, '.  
  5.    'and shows it on every page.');  
  6. //ÉèÖÃÒýÓÃ½Úµã  
  7. $allContents = $dom->getElementsByTagName('publisher');  
  8. $contents = $allContents->item(0);  
  9. //µ÷ÓÃinsertNewChildº¯Êý   
  10. insertNewChild($contents,$newElement);  
  11. //Õâ¸öº¯Êý²åÈëÒ»¸öеÄ×Ó½Úµã×÷Ϊ $currentNodeµÄµÚÒ»¸ö×Ó½Úµã  
  12. function insertNewChild($currentNode, $node)  
  13. {  
  14.    $currentNode->insertBefore(  
  15.       $node, $currentNode->firstChild);     

ÏÔʾеĽڵã

ͨ¹ýgetNodesInfoÔËÐÐÐ޸ĺóµÄÎĵµ£¬ÏÔʾеĽڵ㣬Èçͼ3Ëùʾ¡£

ͼ- 3 ²åÈë½Úµã£ºÕâÕÅͼƬÏÔʾÁËÔÚ<publisher>½ÚµãÇ°²åÈëÁË<foreward>×Ó½Úµã

½Úµã¿Ë¡

¿Ë¡һ¸ö½ÚµãÒâζ×Å´´½¨Ò»¸öºÍµ±Ç°½ÚµãһģһÑùµÄ½Úµã£¬¿Ë¡½ÚµãʱʹÓÃcloneNode·½·¨¡£

DOMNode DOMNode::cloneNode([ bool $deep])£º´´½¨µ±Ç°µÄ¿Ë¡£¬$deep²ÎÊýÖ¸¶¨ÊÇ·ñÒª¿½±´µ±Ç°½ÚµãµÄ×ӽڵ㣬ÆäĬÈÏÖµÊÇfalse¡£ÏÂÃæµÄʾÀý´úÂëÏÔʾ¿Ë¡ <author>ÔªËØ£¬²¢×÷ΪԴ<author>ÔªËصÄ×ӽڵ㣬ͼ4ÏÔʾÁËÊä³öÄÚÈÝ£º

  1. //ÉèÖÃÒýÓÃ½Úµã  
  2. $author = $root->getElementsByTagName('author')->item(0);  
  3. //µ÷ÓÃcloningNodeº¯Êý  
  4. cloningNode($author);   
  5. //Õâ¸öº¯Êý¿Ë¡$currentNode  
  6. function cloningNode($currentNode)  
  7.    {  
  8.       $clonenode = $currentNode -> cloneNode(true);  
  9.       $newnode = $currentNode->appendChild($clonenode);     
  10.    } 

¿Ë¡½Úµã

ͼ- 4 ¿Ë¡½Úµã£º¿Ë¡<author>×ӽڵ㲢½«Æä·ÅÔÚÔ´<author>½ÚµãÏ£¬Ô´½ÚµãµÄÎı¾Öµ±ä³ÉÁ½·ÝÁË£¬ÒòΪ¼ìË÷½ÚµãµÄÎı¾ÖµÊ±Ò²¼ìË÷ÁËÆä×Ó½ÚµãµÄ Öµ

#p#

ÒƳý×Ó½Úµã

ʹÓÃremoveChild·½·¨´ÓDOMÊ÷ÖÐÒƳý×ӽڵ㡣

DOMNode DOMNode::removeChild(DOMNode $oldnode)£ºÕâ¸öº¯ÊýÒƳýÒ»¸ö×ӽڵ㣬$oldnode²ÎÊýÖ¸³öÒªÒƳýµÄ×ӽڵ㡣ÏÂÃæµÄʾÀý´úÂë´ÓBook.xmlÎĵµÖÐÒƳý×Ó ½Úµã£¬´Óͼ5ÖÐÏÔʾµÄÊä³öÄÚÈÝ¿ÉÒÔ¿´µ½bibliography½ÚµãÏûʧÁË¡£

ÒƳý½Úµã

ͼ- 5 ÒƳý½Úµã£ºÒƳý×îºóÒ»¸ö×Ó½Úµã<bibliography>ºó£¬ÖØÐÂÁгöÁ˽ڵãµÄÃû³ÆÖµ£¬ÏÔʾ½Úµãȷʵ±»ÒƳýÁË

Ìæ»»½Úµã

ΪÁËÓÃÒ»¸öнڵãÌæ»»ÒÑÓеĽڵ㣬ʹÓÃreplaceChild·½·¨¡£

DOMNode DOMNode::replaceChild(DOMNode $newnode, DOMNode $oldnode)£ºÕâ¸öº¯ÊýʹÓÃ$newnode×Ó½ÚµãÌæ»»$oldnode½Úµã¡£

ÀýÈ磬¼ÙÉèÄãÏëÓÃеÄcode×Ó½ÚµãÌæ»»ISBN×ӽڵ㣺

  1. //»ñÈ¡ISBN½Úµã  
  2. $element = $dom->getElementsByTagName('ISBN')->item(0);   
  3. //´´½¨ÐµÄ<code>ÔªËØ   
  4. $code = $dom->createElement('code', '909090');  
  5. //µ÷ÓÃreplacingNodeº¯Êý   
  6. replacingNode($code,$element);  
  7. //Õâ¸öº¯ÊýʹÓÃ$nodeÌæ»»$currentNode   
  8. function replacingNode($currentNode, $node)  
  9. {  
  10.    $node->parentNode->replaceChild($currentNode, $node);     

Ìæ»»½Úµã

ͼ6ÖÐÏÔʾµÄÊä³öÄÚÈݱíÃ÷½Úµã±»Ìæ»»ÁË

ͼ- 6 Ìæ»»½Úµã£ºÕâÀïÏÔʾµÄÊÇÓÃеÄ<code>½ÚµãÌæ»»<ISBN>½ÚµãºóµÄÎĵµÄÚÈÝ

µ¼Èë½Úµã

ʹÓÃimportNode·½·¨´ÓÁíÒ»¸öÊ÷¿½±´Ò»¸ö½Úµãµ½µ±Ç°µÄÊ÷¡£

DOMNode DOMDocument::importNode(DOMNode $importedNode [,bool $deep])£ºÕâ¸ö·½·¨´ÓÁíÒ»¸öXMLÎĵµµ¼ÈëÒ»¸ö½Úµã£¬È»ºó²åÈ뵱ǰÎĵµµÄDOMÊ÷ÖУ¬ $importedNode²ÎÊýÖ¸³öÁËÒªµ¼ÈëµÄ½Úµã£¬µ¼ÈëµÄ½Úµã±íʾԭʼ½ÚµãµÄÒ»·Ý¿½±´£¬Òò´Ëµ¼Èë²Ù×÷²»»áÐÞ¸ÄÍⲿÊ÷£¬$deep²ÎÊý¿ØÖÆÊÇ·ñµ¼Èë±»µ¼Èë½ÚµãµÄÉî¶È£¬ÖµÎª TRUEʱ£¬µ¼ÈëÍêÕûµÄ½Úµã×ÓÊ÷£¬ÎªFALSEʱ£¬Ö»µ¼Èë½Úµã±¾Éí¡£

ÏÂÃæµÄʾÀý´ÓBook_continue.xmlÎļþµ¼Èë<continue>½Úµãµ½Book.xml£¬ÏÂÃæÊÇBook_continue.xmlÎĵµµÄÄÚÈÝ£º

  1. <?xml version="1.0" encoding="UTF-8" standalone="yes" ?> 
  2. <!--chapter V--> 
  3. <continue> 
  4.   <chapter_V> 
  5.    <title>XPath</title> 
  6.    <content>XPath is language for...</content> 
  7.   </chapter_V> 
  8.   <![CDATA[  
  9.      This chaper is a bonus to...  
  10.      ]]> 
  11.   <printing cap_I="click_here_for_chapter_I"   
  12.     cap_II="click_here_for_chapter_II"     
  13.     cap_III="click_here_for_chapter_III" 
  14.     cap_IV="click_here_for_chapter_IV"   
  15.     cap_V="click_here_for_chapter_V" /> 
  16. </continue> 
  17. ÏÂÃæÊǵ¼Èë<continue>½ÚµãµÄ´úÂ룺  
  18. <?php 
  19. $olddoc = new DOMDocument;  
  20. $olddoc->load("Book_continue.xml");  
  21. //ÎÒÏëµ¼Èëµ½Ò»¸öÐÂÎĵµµÄ½Úµã  
  22. $node = $olddoc->getElementsByTagName("continue")->item(0);  
  23. $newnewdoc = new DOMDocument;  
  24. $newdoc->formatOutput = true;  
  25. $newdoc->load("Book.xml");  
  26. //µ¼Èë½Úµã¼°ÆäËùÓÐ×ӽڵ㵽Îĵµ  
  27. $node = $newdoc->importNode($node, true);  
  28. //È»ºó×·¼Óµ½¸ù½Úµã  
  29. $newdoc->documentElement->appendChild($node);  
  30. echo "\nThe 'new document' after copying the nodes into it:\n";  
  31. $root = $newdoc->firstChild;  
  32. function getNodesInfo($node)  
  33. {  
  34.    if ($node->hasChildNodes())  
  35.    {  
  36.       $subNodes = $node->childNodes;  
  37.       foreach ($subNodes as $subNode)  
  38.       {  
  39.          if (($subNode->nodeType != 3) ||   
  40.             (($subNode->nodeType ==3) &&  
  41.             (strlen(trim($subNode->wholeText))>=1)))     
  42.          {  
  43.          echo "Node name: ".$subNode->nodeName."\n";  
  44.          echo "Node value: ".$subNode->nodeValue."\n";  
  45.       }  
  46.       getNodesInfo($subNode);           
  47.       }  
  48.    }        
  49. }  
  50. getNodesInfo($root);     
  51. ?> 

µ¼Èë½Úµã

ͼ7ÏÔʾǰÃæ´úÂëµÄÊä³ö

ͼ- 7 µ¼Èë½Úµã£ºÕâÀïÏÔʾÁË´ÓBook_continue.xmlµ¼Èë½Úµã²¢×·¼Óµ½Book.xmlºóµÄÑù×Ó

¼ì²é½ÚµãµÄµÈͬÐÔ

¼ì²éÁ½¸ö½ÚµãÊÇ·ñÏàͬʹÓÃisSameNode·½·¨¡£

bool DOMNode::isSameNode(DOMNode $node)£ºµ±½ÚµãÊÇÏàµÈµÄʱºò£¬Õâ¸öº¯Êý·µ»ØÒ»¸ö²¼¶ûÖµTRUE£¬·ñÔò·µ»ØFALSE£¬$node²ÎÊý±íʾÄãÒªºÍµ±Ç°½Úµã½øÐÐ±È½Ï µÄ½Úµã¡£

×¢Òâ±È½Ï²»ÊÇ»ùÓÚ½ÚµãµÄÄÚÈݽøÐеģº

  1. //¼ì²éÁ½¸ö½ÚµãÊÇ·ñÏàͬ  
  2. $author1 = $root->getElementsByTagName('autor')->item(0);  
  3. $author2 = $root->getElementsByTagName('autor')->item(1);  
  4. //µ÷ÓÃverifyNodesº¯Êý  
  5. verifyNodes($author1,$author2);  
  6. function verifyNodes($currentNode, $node)  
  7. {  
  8.    if (($currentNode->isSameNode($node))==true)  
  9.    {  
  10.       echo "These two nodes are the same";  
  11.    }     

´´½¨ÐµÄÊ÷

PHP 5 DOMÀ©Õ¹¿ÉÒÔÈÃÄã´ÓÁ㿪ʼ¹¹½¨DOMÊ÷£¬ÏÂÃæµÄʾÀý´´½¨ÁËÒ»¸öȫеÄXMLÎĵµ£¬Ê¹ÓÃÁËÁ½¸öк¯Êý´´½¨×¢ÊͺÍCDATA½Úµã¡£

1¡¢DOMComment DOMDocument::createComment(string $data)£º´´½¨Ò»¸öеÄ×¢Êͽڵ㣬$data²ÎÊý±íʾ½ÚµãµÄÄÚÈÝ¡£

2¡¢DOMCDATASection DOMDocument::createCDATASection(string $data)£º´´½¨Ò»¸öеÄCDATA½Úµã£¬$data²ÎÊý±íʾ½ÚµãµÄÄÚÈÝ¡£

  1. <?php   
  2.   //´´½¨Ò»¸öÎĵµÊµÀý   
  3.   $document = new DOMDocument();  
  4.   //ʹÓÃËõ½ø¸ñʽ»¯Êä³ö  
  5.   $document->formatOutput = true;  
  6.   //´´½¨Ò»¸ö×¢ÊÍ  
  7.   $comment = $document->createComment('Beautiful flowers!!!');  
  8.   $document->appendChild( $comment );   
  9.   //´´½¨<flowers>¸ùÔªËØ   
  10.   $root = $document->createElement( 'flowers' );  
  11.   $document->appendChild( $root );  
  12.   //´´½¨<tulips>×Ó½Úµã  
  13.   $tulips = $document->createElement( 'tulips' );  
  14.   //´´½¨<tulips>ÔªËصĵÚÒ»¸ö×Ó½Úµã<bulbs>£¬²¢ÉèÖÃÆäÊôÐÔ  
  15.    $bulbs_1 = $document->createElement( 'bulbs' );     
  16.   $bulbs_1->setAttribute('price','€ 7.65');  
  17.   $bulbs_1->appendChild($document->createTextNode( 'Parrot'));  
  18.   $tulips->appendChild( $bulbs_1 );  
  19.   //´´½¨<tulips>ÔªËصĵڶþ¸ö×Ó½Úµã<bulbs>£¬²¢ÉèÖÃÆäÊôÐÔ  
  20.   $bulbs_2 = $document->createElement( 'bulbs' );      
  21.   $bulbs_2->setAttribute('color','magenta');  
  22.   $bulbs_2->appendChild($document->createTextNode( 'Lily flowering' ));  
  23.   $tulips->appendChild( $bulbs_2 );  
  24.   //×·¼Ó<tulips>½Úµãµ½¸ù½Úµãºó  
  25.   $root->appendChild( $tulips );  
  26.   //´´½¨CDATAС½Ú  
  27.   $cdata = $document->createCDATASection(  
  28.     '<gladiolus><species>Sword Lily</species>'.  
  29.     '<species>Starface</species></gladiolus>');  
  30.   $document->appendChild( $cdata );   
  31.   //±£´æ¶ÔÏóÊ÷µ½Flowers.xml   
  32.   echo $document->saveXML();  
  33.   $document->save('Flowers.xml');  
  34. ?> 
  35. еÄFlower.xmlÎĵµÄÚÈÝÈçÏ£º  
  36. <?xml version="1.0" encoding="ISO-8859-1"?> 
  37. <!--Beautiful flowers!!!--> 
  38. <flowers> 
  39.   <tulips> 
  40.     <bulbs price="€ 7.65">Parrot</bulbs> 
  41.     <bulbs color="magenta">Lily flowering</bulbs> 
  42.   </tulips> 
  43. </flowers> 
  44. <![CDATA [<gladiolus>  
  45.     <species>Sword Lily</species>  
  46.     <species>Starface</species>  
  47.   </gladiolus>  
  48. ]]> 

Çåµ¥2ÖеĴúÂë´´½¨ÁËÒ»¸ö¶ÔÏóÊ÷£¬²¢½«Æä±£´æΪFlowers.xml¡£

Çåµ¥2 ´´½¨Ò»¸öеÄDOMÊ÷

±¾Îļòµ¥½éÉÜÁËPHP 5 DOMÀ©Õ¹£¬²¢½éÉÜÈçºÎʹÓÃËü²Ù×÷XML£¨»òHTML£©Îĵµ£¬ÒÔ¼°ÈçºÎ´ÓÁã´´½¨Ò»¸öDOMÊ÷¡£

¡¾±à¼­ÍƼö¡¿

  1. ¹úÍâÓÅÐ㿪ԴPHP½¨Õ¾³ÌÐòÒ»ÀÀ
  2. PHP 5.3£ºÔ¶´óÓÚ¿ª·¢ÕßÔ¤ÆÚµÄÖØ´ó°æ±¾
  3. PHPnow1.5.3ÏÂÔØ - ÂÌÉ«PHP»·¾³°ü
  4. dz̸ÈçºÎ´î½¨IIS+PHP+JSP+MySQL»·¾³
  5. ¿ª·¢ÀÏÊÖ̸Windowsƽ̨µÄPHPÓ¦Óÿª·¢
责任编辑:彭凡 来源: 51cto.com
相关推荐

2010-09-28 12:59:45

JavaScriptDOM

2010-09-28 11:22:18

Html DOM树

2009-07-15 16:23:54

Java JDBC

2010-09-29 16:38:03

企业应用访问

2011-05-30 13:28:00

PHP

2010-09-09 16:17:10

PHPXMLXML DOM

2012-04-26 08:29:22

DOM

2010-09-08 16:50:11

JavaScriptDOM操作

2011-07-12 17:26:02

PHPPython

2009-06-18 12:21:07

javascriptdom

2021-04-09 18:01:03

前端ReactDOM

2011-06-24 15:57:35

SQL AzureDAC

2009-06-22 11:52:00

javascriptxml

2011-07-26 10:46:04

HTML 5

2010-03-29 13:39:41

ibmdwPHP

2016-11-16 18:49:21

2010-09-09 13:55:47

XML DOM

2009-12-02 14:14:06

PHP DOM-XML

2018-10-06 05:00:53

2009-05-19 10:10:01

Oracle时间加减时间操作
点赞
收藏

51CTO技术栈公众号