Óà PHP ¶ÁÈ¡ºÍ±àд XML DOM
Óà PHP ¶ÁÈ¡ºÍ±àд XML DOM
ʹÓà DOM ¿â¡¢SAX ½âÎöÆ÷ºÍÕýÔò±í´ïʽ
ÎĵµÑ¡Ïî
´òÓ¡±¾Ò³
½«´ËÒ³×÷Ϊµç×ÓÓʼþ·¢ËÍ
¼¶±ð£º Öм¶
Jack Herrington (jack_d_herrington@codegeneration.net), ¸ß¼¶Èí¼þ¹¤³Ìʦ, "Code Generation Network"
2006 Äê 2 ÔÂ 06 ÈÕ
ÓÐÐí¶à¼¼Êõ¿ÉÓÃÓÚÓà PHP ¶ÁÈ¡ºÍ±àд XML¡£±¾ÎÄÌṩÁËÈýÖÖ·½·¨¶ÁÈ¡ XML£ºÊ¹Óà DOM ¿â¡¢Ê¹Óà SAX ½âÎöÆ÷ºÍʹÓÃÕýÔò±í´ïʽ¡£»¹½éÉÜÁËʹÓà DOM ºÍ PHP Îı¾Ä£°å±àд XML¡£
Óà PHP ¶ÁÈ¡ºÍ±àд¿ÉÀ©Õ¹±ê¼ÇÓïÑÔ£¨XML£©¿´ÆðÀ´¿ÉÄÜÓеã¿Ö²À¡£Êµ¼ÊÉÏ£¬XML ºÍËüµÄËùÓÐÏà¹Ø¼¼Êõ¿ÉÄÜÊÇ¿Ö²ÀµÄ£¬µ«ÊÇÓà PHP ¶ÁÈ¡ºÍ±àд XML ²»Ò»¶¨ÊÇÏî¿Ö²ÀµÄÈÎÎñ¡£Ê×ÏÈ£¬ÐèҪѧϰһµã¹ØÓÚ XML µÄ֪ʶ —— ËüÊÇʲô£¬ÓÃËü×öʲô¡£È»ºó£¬ÐèҪѧϰÈçºÎÓà PHP ¶ÁÈ¡ºÍ±àд XML£¬¶øÓÐÐí¶àÖÖ·½Ê½¿ÉÒÔ×öÕâ¼þÊ¡£
±¾ÎÄÌṩÁË XML µÄ¼ò¶ÌÈëÃÅ£¬È»ºó½âÊÍÈçºÎÓà PHP ¶ÁÈ¡ºÍ±àд XML¡£
ʲôÊÇ XML£¿
XML ÊÇÒ»ÖÖÊý¾Ý´æ´¢¸ñʽ¡£ËüûÓж¨Òå±£´æʲôÊý¾Ý£¬Ò²Ã»Óж¨ÒåÊý¾ÝµÄ¸ñʽ¡£XML Ö»ÊǶ¨ÒåÁ˱ê¼ÇºÍÕâЩ±ê¼ÇµÄÊôÐÔ¡£¸ñʽÁ¼ºÃµÄ XML ±ê¼Ç¿´ÆðÀ´ÏñÕâÑù£º
<name>Jack Herrington</name>
Õâ¸ö <name> ±ê¼Ç°üº¬Ò»Ð©Îı¾£ºJack Herrington¡£
²»°üº¬Îı¾µÄ XML ±ê¼Ç¿´ÆðÀ´ÏñÕâÑù£º
<powerUp />
Óà XML ¶Ôij¼þʽøÐбàдµÄ·½Ê½²»Ö¹Ò»ÖÖ¡£ÀýÈ磬Õâ¸ö±ê¼ÇÐγɵÄÊä³öÓëÇ°Ò»¸ö±ê¼ÇÏàͬ£º
<powerUp></powerUp>
Ò²¿ÉÒÔÏò XML ±ê¼ÇÌí¼ÓÊôÐÔ¡£ÀýÈ磬Õâ¸ö <name> ±ê¼Ç°üº¬ first ºÍ last ÊôÐÔ£º
<name first="Jack" last="Herrington" />
Ò²¿ÉÒÔÓà XML ¶ÔÌØÊâ×Ö·û½øÐбàÂë¡£ÀýÈ磬& ·ûºÅ¿ÉÒÔÏñÕâÑù±àÂ룺
&
°üº¬±ê¼ÇºÍÊôÐ﵀ XML ÎļþÈç¹ûÏñʾÀýÒ»Ñù¸ñʽ»¯£¬¾ÍÊǸñʽÁ¼ºÃµÄ£¬ÕâÒâζ×űê¼ÇÊǶԳƵģ¬×Ö·ûµÄ±àÂëÕýÈ·¡£Çåµ¥ 1 ÊÇÒ»·Ý¸ñʽÁ¼ºÃµÄ XML µÄʾÀý¡£
Çåµ¥ 1. XML ͼÊéÁбíʾÀý
<books>
<book>
<author>Jack Herrington</author>
<title>PHP Hacks</title>
<publisher>O'Reilly</publisher>
</book>
<book>
<author>Jack Herrington</author>
<title>Podcasting Hacks</title>
<publisher>O'Reilly</publisher>
</book>
</books>
Çåµ¥ 1 ÖÐµÄ XML °üº¬Ò»¸öͼÊéÁÐ
Ïà¹ØÎĵµ£º
Output Control º¯Êý¿ÉÒÔÈÃÄã×ÔÓÉ¿ØÖƽű¾ÖÐÊý¾ÝµÄÊä³ö¡£Ëü·Ç³£µØÓÐÓã¬ÌرðÊǶÔÓÚ£ºµ±ÄãÏëÔÚÊý¾ÝÒѾÊä³öºó£¬ÔÙÊä³öÎļþÍ·µÄÇé¿ö¡£Êä³ö¿ØÖƺ¯Êý²»¶ÔʹÓà header() »ò setcookie(), ·¢Ë͵ÄÎļþÍ·ÐÅÏ¢²úÉúÓ°Ïì,Ö»¶ÔÄÇЩÀàËÆÓÚ echo() ºÍ PHP ´úÂëµÄÊý¾Ý¿éÓÐ×÷Óá£
ÎÒÃÇÏȾÙÒ»¸ö¼òµ¥µÄÀý×Ó£¬Èôó¼Ò¶ÔOutput ControlÓÐÒ»¸ö´ ......
ΪÁËÉîÈëÁ˽âPHP£¬ÒÔ¼°¸ü¼Ó±ãÀûµÄʹÓÃPHPΪÎÒÃÇ·þÎñ£¬±¾Õ½ÚÖ÷Òª½éÉÜPHPµÄ»ù±¾Óï·¨¡£
Ò»¡¢´ÓHTMLÖзÖÀë
PHP µÄ½Å±¾ÓÉÎļþÖÐÒ»¶ÔÌØÊâµÄ±ê¼ÇËùÒýÆðµÄÄÚÈÝ£¬µ±½âÊÍÆ÷·ÖÎöÒ»¸öÎļþʱ£¬ËùÓÐÌØÊâ±ê¼ÇÍâµÄÆÕͨÎļþ£¬½âÊÍÆ÷²»¶ÔÆä×öÈκδ¦Àí£¬¶øÌØÊâ±ê¼ÇÄÚÎļþ½«×÷Ϊ PHP ´úÂë±»½âÊÍÆ÷·ÖÎöÖ´ÐС£¸ÃÔËÐлúÖƽ«ÔÊÐíÄúÔÚ HTML ÖÐǶÈë ......
php¶ÔxmlÎļþÖнڵãµÄɾ³ý\±à¼
ʹÓÃgetElementsByTagName()·½·¨É¾³ý\±à¼XML½Úµã
Èý¸öÎļþ£º
1.root.xml
2.remove.php —— ɾ³ý½Úµã
3.replace.php —— Ìæ»»½Úµã
root.xml
<root>
<child1>child1 content</child1>
<child2>child2 content</child2>
& ......
ÔÚphpÖмÆËãʱ¼ä²îÓÐʱºòÊǼþÂé·³µÄÊÂ!²»¹ýÖ»ÒªÄãÕÆÎÕÁËÈÕÆÚʱ¼äº¯ÊýµÄÓ÷¨ÄÇÕâЩҲ¾Í±äµÄ¼òµ¥ÁË:
Ò»¸ö¼òµ¥µÄÀý×Ó¾ÍÊǼÆËã½èÊéµÄÌìÊý,ÕâÐèÒªphp¸ù¾ÝÿÌìµÄÈÕÆÚ½øÐмÆËã,ÏÂÃæ¾ÍÀ´Ì¸Ì¸ÊµÏÖÕâÖÖÈÕÆÚ¼ÆËãµÄ¼¸ÖÖ·½·¨:
(1) Èç¹ûÓÐÊý¾Ý¿â¾ÍºÜÈÝÒ×ÁË!ÈôÊÇMSSQL¿ÉÒÔʹÓô¥·¢Æ÷!ÓÃרÃżÆËãÈÕÆÚ²îµÄº¯Êýdatediff()±ã¿É ......