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
- <?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
- <book>
- <!--XML Processing [part I] -->
- <name>XML Processing I</name>
- <author>John Smith Jr.</author>
- <publisher>HisOwnTM</publisher>
- <ISBN>111-222-333-4441</ISBN>
- <contents>
- <chapter_I>
- <title>What is XML about ?</title>
- <content>XML (Extensible Markup Language) is a ...</content>
- </chapter_I>
- <chapter_II>
- <title>SAX</title>
- <content>SAX is a simple API for ...</content>
- </chapter_II>
- <chapter_III>
- <title>StAX</title>
- <content>Much powerful and flexible, StAX, is very...</content>
- </chapter_III>
- <chapter_IV>
- <title>DOM
- <subtitle>DOM concept
- <continut>Starting to use DOM...</continut>
- </subtitle>
- <subchapter_IV_I>
- <title>First DOM application...</title>
<content>Here it is your first DOM application...</content> - </subchapter_IV_I>
- </title>
- </chapter_IV>
- <end>The end...</end>
- </contents>
- <!-- See you in XML Processing [part II] -->
- </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>ÔªËØ£¬Ñ¡Ôñÿ¸ö×Ó½ÚµãµÄµÚÒ»¸ö£¬×îºó´òÓ¡ÕâЩ½ÚµãµÄÖµ£º
- <?php
- // ´´½¨Ò»¸öÎĵµÊµÀý
- $doc = new DOMDocument();
- //ÔØÈëBook.xmlÎļþ
- $doc->load( 'Book.xml' );
- //ʹÓÃbook±êÇ©ÃûËÑË÷ËùÓÐÔªËØ
- $books = $doc->getElementsByTagName( "book" );
- //ʹÓÃauthor±êÇ©ÃûËÑË÷ËùÓÐÔªËØ
- $authors = $doc->getElementsByTagName( "author" );
- //·µ»ØµÚÒ»¸ö±êÇ©ÃûΪauthorµÄÔªËØ
- $author = $authors->item(0)->nodeValue;
- //ÒÔpublisher±êÇ©ÃûËÑË÷ËùÓÐÔªËØ
- $publishers = $doc->getElementsByTagName( "publisher" );
- //·µ»ØµÚÒ»¸öÕÒµ½µÄ±êÇ©ÃûΪpublisherµÄÔªËØ
- $publisher = $publishers->item(0)->nodeValue;
- //ËÑË÷±êÇ©ÃûΪnameµÄËùÓÐÔªËØ
- $titles = $doc->getElementsByTagName( "name" );
- //·µ»Ø±êÇ©ÃûΪnameµÄµÚÒ»¸öÕÒµ½µÄÔªËØ
- $title = $titles->item(0)->nodeValue;
- //´òÓ¡ÕÒµ½µÄÖµ
- echo "$title - $author - $publisher \n";
- ?>
×îºóÒ»ÐÐÊÇ´òÓ¡µÚÒ»¸ö±êÌ⣬µÚÒ»¸ö×÷Õߣ¬µÚÒ»¸ö³ö°æÉÌ£¬Ê¹ÓÃÁ¬×Ö·û·Ö¸ô£¬Êä³ö£º
XML Processing I - John Smith Jr. - HisOwnTM
µÝ¹éä¯ÀÀDOMÊ÷
ÒòΪXMLÎĵµ½á¹¹ÖÐÒ»¸ö±êÇ©¿ÉÒÔ°üÀ¨ÁíÒ»¸ö±êÇ©£¨·ÖÖ§Ê÷£©£¬Ê£Ï¾ÍÊÇÒ¶×ӽڵ㣬Òò´ËÄã¿ÉÒÔä¯ÀÀÍêÕûµÄÊ÷»ò´ÓÈκνڵ㿪ʼµÝ¹éä¯ÀÀ×ÓÊ÷ ¡£ÏÂÃæµÄÀý×ÓÊÇ´ÓÈκοªÊ¼½Úµã£¨$node£©ä¯ÀÀÏÂÃæµÄXML×ÓÊ÷£¬²¢Áгö½ÚµãµÄÃû×ÖºÍÖµ¡£
- function getNodesInfo($node)
- {
- if ($node->hasChildNodes())
- {
- $subNodes = $node->childNodes;
- foreach ($subNodes as $subNode)
- {
- if (($subNode->nodeType != 3) ||
- (($subNode->nodeType == 3) &&
- (strlen(trim($subNode->wholeText))>=1)))
- {
- echo "Node name: ".$subNode->nodeName."\n";
- echo "Node value: ".$subNode->nodeValue."\n";
- }
- getNodesInfo($subNode);
- }
- }
- }
ÉÏÃæµÄÀý×ÓʹÓÃÏÂÃæµÄÌõ¼þÈ¥³ýÁËËùÓпÕÎı¾½Úµã£¬ÈÃÊä³ö¿´ÆðÀ´¸ü¸É¾»£º
- if (($subNode->nodeType != 3) ||
- (($subNode->nodeType == 3) &&
- (strlen(trim($subNode->wholeText))>=1)))
Ç°ÃæµÄ´úÂë¼ì²é½ÚµãÊÇ·ñ±»´¦Àí£¬Í¬Ñù£¬Äã¿ÉÒÔÉèÖÃÔ¤¶¨ÒåµÄpreserveWhiteSpaceÊôÐÔ£¬ËüÒƳýÈßÓàµÄ¿Õ°×£¬Ä¬ÈÏÖµÊÇTRUE¡£
ΪÁ˲âÊÔÕâ¸ö¹¦ÄÜ£¬ÏÂÃæÕâ¸öÀý×Ó´«µÝBook.xmlÎĵµµÄ¸ù½Úµã¸øµÝ¹éº¯ÊýgetNodesInfo£¬È»ºó´òÓ¡³öÕû¸öDOMÊ÷µÄ±êÇ©ºÍÖµ£º
- <?php
- //´´½¨Ò»¸öÎĵµÊµÀý
- $doc = new DOMDocument();
- //ÔØÈëBook.xmlÎļþ
- $doc->load( 'Book.xml' );
- //ÉèÖöÔÏóÊ÷¸ù
- $root = $dom->firstChild;
- // µÝ¹éº¯ÊýÁгö×ÓÊ÷µÄËùÓнڵã
- function getNodesInfo($node)
- {
- if ($node->hasChildNodes())
- {
- $subNodes = $node->childNodes;
- foreach ($subNodes as $subNode)
- {
- if (($subNode->nodeType != 3) ||
- (($subNode->nodeType == 3)
- &&(strlen(trim($subNode->wholeText))>=1)))
- {
- echo "Node name: ".$subNode->nodeName."\n";
- echo "Node value: ".$subNode->nodeValue."\n";
- }
- getNodesInfo($subNode);
- }
- }
- }
- //µ÷ÓÃgetNodesInfoº¯Êý
- getNodesInfo($root);
- ?>
ͼ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>½Úµã£¬²¢½«Æä×·¼Óµ½½ÚµãµÄĩβ£º
- //´´½¨Ò»¸öÐÂÔªËØ
- $newElement = $dom->createElement('bibliography','Martin Didier, Professional XML');
- //ʹÓÃappendChildº¯Êý½«Æä×·¼Óµ½¸ù½Úµã
- //µ÷ÓÃappendChildº¯Êý
- appendNewChild($root,$newElement);
- //Õâ¸öº¯Êý×·¼ÓÁËÒ»¸öеÄ×Ó½Úµã
- function appendNewChild($currentNode, $node)
- {
- $currentNode->appendChild($node);
- }
Èç¹ûÄãͨ¹ýgetNodeInfo()º¯ÊýÔËÐеóö½á¹û£¬Ä㽫»á¿´µ½Èçͼ2ËùʾµÄÊä³ö¡£
ͼ- 2 ×·¼ÓµÄ½Úµã£ºÕâ¸öͼÏÔʾÁËмӵÄ<bibliography>½ÚµãºÍËüµÄÄÚÈÝ
ÏÂÃæµÄÀý×ÓÊÇÔÚ<publisher>½ÚµãÔö¼ÓÒ»¸ö<foreword>×ӽڵ㣺
- //´´½¨Ò»¸öеÄ<foreword>ÔªËØ
- $newElement = $dom->createElement('foreword',
- 'What I love about this book is that it '.
- 'grew out of just such a process, '.
- 'and shows it on every page.');
- //ÉèÖÃÒýÓýڵã
- $allContents = $dom->getElementsByTagName('publisher');
- $contents = $allContents->item(0);
- //µ÷ÓÃinsertNewChildº¯Êý
- insertNewChild($contents,$newElement);
- //Õâ¸öº¯Êý²åÈëÒ»¸öеÄ×Ó½Úµã×÷Ϊ $currentNodeµÄµÚÒ»¸ö×Ó½Úµã
- function insertNewChild($currentNode, $node)
- {
- $currentNode->insertBefore(
- $node, $currentNode->firstChild);
- }
ͨ¹ýgetNodesInfoÔËÐÐÐ޸ĺóµÄÎĵµ£¬ÏÔʾеĽڵ㣬Èçͼ3Ëùʾ¡£
ͼ- 3 ²åÈë½Úµã£ºÕâÕÅͼƬÏÔʾÁËÔÚ<publisher>½ÚµãÇ°²åÈëÁË<foreward>×Ó½Úµã
½Úµã¿Ë¡
¿Ë¡һ¸ö½ÚµãÒâζ×Å´´½¨Ò»¸öºÍµ±Ç°½ÚµãһģһÑùµÄ½Úµã£¬¿Ë¡½ÚµãʱʹÓÃcloneNode·½·¨¡£
DOMNode DOMNode::cloneNode([ bool $deep])£º´´½¨µ±Ç°µÄ¿Ë¡£¬$deep²ÎÊýÖ¸¶¨ÊÇ·ñÒª¿½±´µ±Ç°½ÚµãµÄ×ӽڵ㣬ÆäĬÈÏÖµÊÇfalse¡£ÏÂÃæµÄʾÀý´úÂëÏÔʾ¿Ë¡ <author>ÔªËØ£¬²¢×÷ΪԴ<author>ÔªËصÄ×ӽڵ㣬ͼ4ÏÔʾÁËÊä³öÄÚÈÝ£º
- //ÉèÖÃÒýÓýڵã
- $author = $root->getElementsByTagName('author')->item(0);
- //µ÷ÓÃcloningNodeº¯Êý
- cloningNode($author);
- //Õâ¸öº¯Êý¿Ë¡$currentNode
- function cloningNode($currentNode)
- {
- $clonenode = $currentNode -> cloneNode(true);
- $newnode = $currentNode->appendChild($clonenode);
- }
ͼ- 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×ӽڵ㣺
- //»ñÈ¡ISBN½Úµã
- $element = $dom->getElementsByTagName('ISBN')->item(0);
- //´´½¨ÐµÄ<code>ÔªËØ
- $code = $dom->createElement('code', '909090');
- //µ÷ÓÃreplacingNodeº¯Êý
- replacingNode($code,$element);
- //Õâ¸öº¯ÊýʹÓÃ$nodeÌæ»»$currentNode
- function replacingNode($currentNode, $node)
- {
- $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ÎĵµµÄÄÚÈÝ£º
- <?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
- <!--chapter V-->
- <continue>
- <chapter_V>
- <title>XPath</title>
- <content>XPath is language for...</content>
- </chapter_V>
- <![CDATA[
- This chaper is a bonus to...
- ]]>
- <printing cap_I="click_here_for_chapter_I"
- cap_II="click_here_for_chapter_II"
- cap_III="click_here_for_chapter_III"
- cap_IV="click_here_for_chapter_IV"
- cap_V="click_here_for_chapter_V" />
- </continue>
- ÏÂÃæÊǵ¼Èë<continue>½ÚµãµÄ´úÂ룺
- <?php
- $olddoc = new DOMDocument;
- $olddoc->load("Book_continue.xml");
- //ÎÒÏëµ¼Èëµ½Ò»¸öÐÂÎĵµµÄ½Úµã
- $node = $olddoc->getElementsByTagName("continue")->item(0);
- $newnewdoc = new DOMDocument;
- $newdoc->formatOutput = true;
- $newdoc->load("Book.xml");
- //µ¼Èë½Úµã¼°ÆäËùÓÐ×ӽڵ㵽Îĵµ
- $node = $newdoc->importNode($node, true);
- //È»ºó×·¼Óµ½¸ù½Úµã
- $newdoc->documentElement->appendChild($node);
- echo "\nThe 'new document' after copying the nodes into it:\n";
- $root = $newdoc->firstChild;
- function getNodesInfo($node)
- {
- if ($node->hasChildNodes())
- {
- $subNodes = $node->childNodes;
- foreach ($subNodes as $subNode)
- {
- if (($subNode->nodeType != 3) ||
- (($subNode->nodeType ==3) &&
- (strlen(trim($subNode->wholeText))>=1)))
- {
- echo "Node name: ".$subNode->nodeName."\n";
- echo "Node value: ".$subNode->nodeValue."\n";
- }
- getNodesInfo($subNode);
- }
- }
- }
- getNodesInfo($root);
- ?>
ͼ7ÏÔʾǰÃæ´úÂëµÄÊä³ö
ͼ- 7 µ¼Èë½Úµã£ºÕâÀïÏÔʾÁË´ÓBook_continue.xmlµ¼Èë½Úµã²¢×·¼Óµ½Book.xmlºóµÄÑù×Ó
¼ì²é½ÚµãµÄµÈͬÐÔ
¼ì²éÁ½¸ö½ÚµãÊÇ·ñÏàͬʹÓÃisSameNode·½·¨¡£
bool DOMNode::isSameNode(DOMNode $node)£ºµ±½ÚµãÊÇÏàµÈµÄʱºò£¬Õâ¸öº¯Êý·µ»ØÒ»¸ö²¼¶ûÖµTRUE£¬·ñÔò·µ»ØFALSE£¬$node²ÎÊý±íʾÄãÒªºÍµ±Ç°½Úµã½øÐÐ±È½Ï µÄ½Úµã¡£
×¢Òâ±È½Ï²»ÊÇ»ùÓÚ½ÚµãµÄÄÚÈݽøÐеģº
- //¼ì²éÁ½¸ö½ÚµãÊÇ·ñÏàͬ
- $author1 = $root->getElementsByTagName('autor')->item(0);
- $author2 = $root->getElementsByTagName('autor')->item(1);
- //µ÷ÓÃverifyNodesº¯Êý
- verifyNodes($author1,$author2);
- function verifyNodes($currentNode, $node)
- {
- if (($currentNode->isSameNode($node))==true)
- {
- echo "These two nodes are the same";
- }
- }
´´½¨ÐµÄÊ÷
PHP 5 DOMÀ©Õ¹¿ÉÒÔÈÃÄã´ÓÁ㿪ʼ¹¹½¨DOMÊ÷£¬ÏÂÃæµÄʾÀý´´½¨ÁËÒ»¸öȫеÄXMLÎĵµ£¬Ê¹ÓÃÁËÁ½¸öк¯Êý´´½¨×¢ÊͺÍCDATA½Úµã¡£
1¡¢DOMComment DOMDocument::createComment(string $data)£º´´½¨Ò»¸öеÄ×¢Êͽڵ㣬$data²ÎÊý±íʾ½ÚµãµÄÄÚÈÝ¡£
2¡¢DOMCDATASection DOMDocument::createCDATASection(string $data)£º´´½¨Ò»¸öеÄCDATA½Úµã£¬$data²ÎÊý±íʾ½ÚµãµÄÄÚÈÝ¡£
- <?php
- //´´½¨Ò»¸öÎĵµÊµÀý
- $document = new DOMDocument();
- //ʹÓÃËõ½ø¸ñʽ»¯Êä³ö
- $document->formatOutput = true;
- //´´½¨Ò»¸ö×¢ÊÍ
- $comment = $document->createComment('Beautiful flowers!!!');
- $document->appendChild( $comment );
- //´´½¨<flowers>¸ùÔªËØ
- $root = $document->createElement( 'flowers' );
- $document->appendChild( $root );
- //´´½¨<tulips>×Ó½Úµã
- $tulips = $document->createElement( 'tulips' );
- //´´½¨<tulips>ÔªËصĵÚÒ»¸ö×Ó½Úµã<bulbs>£¬²¢ÉèÖÃÆäÊôÐÔ
- $bulbs_1 = $document->createElement( 'bulbs' );
- $bulbs_1->setAttribute('price',' 7.65');
- $bulbs_1->appendChild($document->createTextNode( 'Parrot'));
- $tulips->appendChild( $bulbs_1 );
- //´´½¨<tulips>ÔªËصĵڶþ¸ö×Ó½Úµã<bulbs>£¬²¢ÉèÖÃÆäÊôÐÔ
- $bulbs_2 = $document->createElement( 'bulbs' );
- $bulbs_2->setAttribute('color','magenta');
- $bulbs_2->appendChild($document->createTextNode( 'Lily flowering' ));
- $tulips->appendChild( $bulbs_2 );
- //×·¼Ó<tulips>½Úµãµ½¸ù½Úµãºó
- $root->appendChild( $tulips );
- //´´½¨CDATAС½Ú
- $cdata = $document->createCDATASection(
- '<gladiolus><species>Sword Lily</species>'.
- '<species>Starface</species></gladiolus>');
- $document->appendChild( $cdata );
- //±£´æ¶ÔÏóÊ÷µ½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>
- ]]>
Çåµ¥2ÖеĴúÂë´´½¨ÁËÒ»¸ö¶ÔÏóÊ÷£¬²¢½«Æä±£´æΪFlowers.xml¡£
Çåµ¥2 ´´½¨Ò»¸öеÄDOMÊ÷
±¾Îļòµ¥½éÉÜÁËPHP 5 DOMÀ©Õ¹£¬²¢½éÉÜÈçºÎʹÓÃËü²Ù×÷XML£¨»òHTML£©Îĵµ£¬ÒÔ¼°ÈçºÎ´ÓÁã´´½¨Ò»¸öDOMÊ÷¡£
¡¾±à¼ÍƼö¡¿