½â¶ÁPHP DOMDocumentÔÚ½âÎöXMLÎļþÖеÄ×÷ÓÃ
½â¶ÁPHP DOMDocumentÔÚ½âÎöXMLÎļþÖеÄ×÷ÓÃ
http://developer.51cto.com 2009-12-02 10:39 ØýÃû Áø³Ç²©¿Í ÎÒÒªÆÀÂÛ(0)
PHP DOMDocumentµÄ¹¦Äܷdz£Ç¿´ó£¬ÎÒÃÇÔÚÕâƪÎÄÕÂÖн«½éÉÜÈçºÎÕýÈ·µÄÔËÓÃPHP DOMDocumentÀ´½øÐÐXMLÎļþµÄ½âÎö¡£Ï£Íû¶ÔÓÖÐèÒªµÄÅóÓÑÓÐËù°ïÖú¡£
ÔÚʹÓÃPHP¶ÔXMLÎļþ½øÐнâÎöµÄʱºò£¬ÎÒÃÇ¿ÉÒÔÓúܶ෽·¨¡£ÔÚÕâÀÎÒÃǽ«Îª´ó¼ÒÖصã½éÉÜÈçºÎʹÓÃPHP DOMDocumentÀ´¼°ÄæÐÐÄܹ»XMLÎļþµÄ½âÎö¡£
¹ØÓÚÓõ½µÄXMLÎļþ»¹ÒÔ“Php Xml´¦ÀíÖ®simplexmlʹÓ÷½·¨Ç³Ì¸”Ò»ÎÄÖеÄXMLΪÀý£¬ÎļþÃûΪ£ºme.xml¡£´úÂëÈçÏ£º
PHP XML´¦ÀíXML´úÂë
<?xml version="1.0" encoding="utf-8"?>
<phplamp>
<post>
<title id="1">PHP XML´¦Àí½éÉÜÒ»</title>
<details>ÏêϸÄÚÈÝÒ»</details>
</post>
<post>
<title id="2">PHP XML´¦Àí½éÉܶþ</title>
<details>ÏêϸÄÚÈݶþ</details>
</post>
<post>
<title id="3">PHP XML´¦Àí½éÉÜÈý</title>
<details>ÏêϸÄÚÈÝÈý</details>
</post>
</phplamp>
È»ºó¾ÍÐèÒªÓÃPHP DOMDocumentÈ¥´¦ÀíÕâ¸öÎļþ£¬½«XMLÎļþÖеÄÈκÎÔªËض¼½âÎö³öÀ´¡£´úÂëÈçÏ£¬ÀïÃæÓÐ×¢ÊÍ¡£
PHP DOMDocument½âÎöXMLÎļþµÄ´úÂë
<?php
// Ê×ÏÈÒª½¨Ò»¸öDOMDocument¶ÔÏó
$xml = new DOMDocument();
// ¼ÓÔØXmlÎļþ
$xml->load("me.xml");
// »ñÈ¡ËùÓеÄpost±êÇ©
$postDom = $xml->getElementsByTagName("post");
// Ñ»·±éÀúpost±êÇ©
foreach($postDom as $post){
// »ñÈ¡Title±êÇ©Node
$title = $post->getElementsByTagName("title");
/**
* Òª»ñÈ¡Title±êÇ©µÄIdÊôÐÔÒª·ÖÁ½²¿×ß
* 1. »ñÈ¡titleÖÐËùÓÐÊôÐÔµÄ
ÁбíÒ²¾ÍÊÇ$title->item(0)->attributes
* 2. »ñÈ¡titleÖÐidµÄÊôÐÔ£¬
ÒòΪÆäÔÚµÚһλËùÒÔÓÃitem(0)
*
* СÌáʾ£º
* ÈôÈ¡ÊôÐÔµÄÖµ¿ÉÒÔÓÃitem(*)->nodeValue
* ÈôÈ¡ÊôÐԵıêÇ©¿ÉÒÔÓÃitem(*)->nodeN
Ïà¹ØÎĵµ£º
Ϊ PHP Ìí¼ÓÀ©Õ¹¿âÊÇÒ»¼þ¼òµ¥µÄÊÂÇé¡£²»¹ýÊ×ÏÈÄã×îºÃ»áÒ»µãµãC/C++±à³Ì¼¼Êõ£¬²¢ÇÒ¶ÔMakefileÓÐÒ»¶¨µÄÁ˽⡣
ÒÔÏÂÊÇÔÚ Linux ϵÄÒ»¸ö¼òµ¥Àý×Ó£º
Ê×ÏÈ£¬½øÈë php Ô´´úÂëĿ¼£¨¼ÙÉèΪ PHP_SRC£¬ÒÔÏÂͬ£©£¬Ê¹Óà php ×Ô´øµÄ ext_skel ¹¤¾ßÉú³ÉÒ»¸öÐÂÀ©Õ¹ mk4php£º
cd PHP_SRC/etc;
./ext_skel --extname=mk4php
Õý³£Ç ......
¡¡Ã¿¸öPHP³ÌÐòÔ±¶¼ÖªµÀPHPÓÐÇ¿´óµÄÕýÔò±í´ïʽ¹¦ÄÜ£¬ÎªÁËÒÔºóµÄ¹¤×÷·½±ã£¬ÎÒ´ÓÍøÉÏÕûÀíÁ˹ØÓÚÕýÔò±í´ïʽµÄ×ÊÁÏ£¬·½±ãÒÔºó¹¤×÷ʱµÄ½øÐÐ×ÊÁϲéÔÄ¡£
¡¡¡¡ÕýÔò±í´ïʽ(regular expression)ÃèÊöÁËÒ»ÖÖ×Ö·û´®Æ¥ÅäµÄģʽ£¬¿ÉÒÔÓÃÀ´¼ì²éÒ»¸ö´®ÊÇ·ñº¬ÓÐijÖÖ×Ó´®¡¢½«Æ¥ÅäµÄ×Ó´®×öÌæ»»»òÕß´Óij¸ö´®ÖÐÈ¡³ö·ûºÏij¸öÌõ¼þµÄ×Ó´®µÈ¡£
¡¡¡¡ ......
Óà PHP ¶ÁÈ¡ºÍ±àд XML DOM
ʹÓà DOM ¿â¡¢SAX ½âÎöÆ÷ºÍÕýÔò±í´ïʽ
ÎĵµÑ¡Ïî
´òÓ¡±¾Ò³
½«´ËÒ³×÷Ϊµç×ÓÓʼþ·¢ËÍ
¼¶±ð£º Öм¶
Jack Herrington (jack_d_herrington@codegeneration.net), ¸ß¼¶Èí¼þ¹¤³Ìʦ, "Code Generation Network"
2006 Äê 2 ÔÂ 06 ÈÕ
ÓÐÐí¶à¼¼Êõ¿ÉÓÃÓÚÓà PHP ¶ÁÈ¡ºÍ±àд XML¡£±¾ÎÄÌṩÁËÈýÖÖ ......
ʹÓà PHP ´¦Àí XML ÅäÖÃÎļþ
ʹÓà XML ÅäÖÃÎļþÇáÒ×µØÅäÖà PHP Ó¦ÓóÌÐòºÍ¶ÔÏó
¼¶±ð£º Öм¶
Vikram Vaswani, ´´Ê¼ÈË, Melonfire
2007 Äê 11 ÔÂ 29 ÈÕ
XML ΪӦÓóÌÐòÅäÖÃÎļþÌṩÁËÒ»ÖÖ±ã½Ý¡¢Ò×Óõıí´ïÓïÑÔ¡£µ«ÓÐʱºò½«ÕâЩÐÅÏ¢ÌáÈ¡µ½ PHP ½Å±¾Öн«»áÃæ¶ÔÒ»¸ö²»Ð¡µÄÌôÕ½¡£ÕâÕýÊÇ XJConf for PHP °ü³öÏÖµÄÔÒò£ºËüÌá ......
PHPÖ§³Ö8ÖÖÔʼÀàÐÍ£¬ÆäÖаüÀ¨£º
4ÖÖ±êÁ¿ÀàÐÍ£ºboolean(²¼¶ûÐÍ)¡¢integer(ÕûÐÍ)¡¢float(¸¡µãÐÍ)¡¢string(×Ö·ûÐÍ)£»
2ÖÖ¸´ºÏÀàÐÍ£ºarray(Êý×é)¡¢object(¶ÔÏó)£»
2ÖÖÌØÊâÀàÐÍ£ºresource(×ÊÔ´)¡¢NULL
Äú¿ÉÄÜ»¹»á¶Áµ½Ò»Ð©¹ØÓÚ“Ë«¾«¶È£¨double£©”ÀàÐ͵IJο¼¡£Êµ¼ÊÉÏ double ºÍ float ÊÇÏàͬµÄ£¬ÓÉÓÚһЩÀúÊ·µÄÔ ......