ѧϰ 10 ÖÖÁ¼ºÃµÄ XML ʹÓÃÏ°¹ß(ת)
Martin C. Brown (questions@mcslp.com), ¿ª·¢ÈËÔ±ºÍ×÷¼Ò
2008 Äê 6 ÔÂ 16 ÈÕ
ʹÓñ¾ÎĽéÉÜµÄ 10 ¸ö¼¼Çɸü¼ÓÇáËɵش¦Àí XML£¬´Ó¶ø¼õÉÙ´íÎó²¢Ìá¸ßЧÂÊ¡£
¼ò½é
ÈËÃǶ¼ºÜϲ»¶ XML ÒÔ¼°ËüËùÌṩµÄÁé»îÐԺͻ¥²Ù×÷ÐÔ£¬µ«ÊÇ£¬Í¨¹ýʹÓÃһЩ¼¼ÇÉ£¬¿ÉÒÔʹÓë XML µÄ»¥²Ù×÷ºÍÓë XML Ò»ÆðʹÓõŤ¾ß¸ü¼Ó¼òµ¥¡£ÔÚ´¦Àí XML ʱÑø³ÉһЩÁ¼ºÃµÄÏ°¹ß¿ÉÒÔ±£Ö¤×î¸ßЧµØÀûÓÃÄúµÄ XML ÎĵµºÍÓ¦ÓóÌÐò¡£
ʹÓà 10 ÖÖÁ¼ºÃµÄÏ°¹ß
ÒÔÏÂÊÇ 10 ÖÖ×îÁ¼ºÃµÄ XML Ï°¹ß£º
¶¨Òå XML ºÍ±àÂë
ʹÓà DTD »ò XSD
¼ÇµÃ½øÐÐÑéÖ¤
ÑéÖ¤²¢²»×ÜÊÇÄܹ»½â¾öÎÊÌâ
XML ½á¹¹ºÍÊôÐÔ
ʹÓà XPath ²éÕÒÐÅÏ¢
²¢²»×ÜÊÇÐèҪʹÓýâÎöÆ÷ÌáÈ¡ÐÅÏ¢
ºÎʱʹÓà SAX ¶ø·Ç DOM ½âÎö
ºÎʱʹÓà DOM ¶ø·Ç SAX ½âÎö
ʹÓÃÁ¼ºÃµÄ XML ±à¼Æ÷
¾³£Ê¹ÓõÄËõд´Ê
DOM£ºÎĵµ¶ÔÏóÄ£ÐÍ£¨Document Object Model£©
DTD£ºÎĵµÀàÐͶ¨Ò壨Document Type Definition£©
HTML£º³¬Îı¾±ê¼ÇÓïÑÔ£¨Hypertext Markup Language£©
IDE£º¼¯³É¿ª·¢»·¾³£¨Integrated Development Environment£©
SAX£ºXML ¼òµ¥ API£¨Simple API for XML£©
XSD£ºXML ģʽ¶¨Ò壨XML Schema Definition£©
XML£º¿ÉÀ©Õ¹±ê¼ÇÓïÑÔ£¨Extensible Markup Language£©
XSLT£º¿ÉÀ©Õ¹Ñùʽ±íÓïÑÔת»»£¨Extensible Stylesheet Language Transformations£©
¶¨Òå XML ºÍ±àÂë
ÔÚ¿ìËÙ´´½¨ XML ÎĵµÊ±£¬Ò»°ã¶¼»áÇãÏòÓÚ´´½¨»ù±¾µÄ½á¹¹²¢±Ü¿ªÒ»Ð©ÆÕͨ XML ÎĵµÐèÇ󣬰üÀ¨Ö¸¶¨ XML ÎĵµÉùÃ÷ºÍ XML Îĵµ°üº¬µÄÊý¾ÝµÄ±àÂëÀàÐÍ¡£
¿¼ÂÇÇåµ¥ 1 ËùʾµÄ XML Îĵµ¡£
Çåµ¥ 1. δ°üº¬ XML ÉùÃ÷ºÍÊý¾Ý±àÂëÀàÐ굀 XML Îĵµ
<phrases>
<phrase lang="en">Hello</phrase>
<phrase lang="it">Buongiorno</phrase>
<phrase lang="fr">Salut!</phrase>
</phrases>
¶ÔÓÚÆÕͨÈËÀ´Ëµ£¬¿ÉÒԲ鿴¸ÃÎĵµ²¢½«Æäʶ±ðΪ XML£¬µ«ÊǶÔÓÚ¼ÆËã»úÀ´Ëµ£¬ÔòºÜÄÑ×÷³öÕâÑùµÄÅжϡ£ÔÚÎļþ¶¥²¿Ìí¼Ó XML ÉùÃ÷£¬¿ÉÒÔʹËü¸ü¼ÓÃ÷È·¡¢¸üÈÝÒ×ʶ±ð¡£Ò»Ðмòµ¥µÄ´úÂë¾Í¿ÉÒÔ˵Ã÷ÎĵµÊÇ XML£¬²¢ÇÒÖ¸³ö°æ±¾ºÅºÍ XML Êý¾ÝʹÓõÄ×Ö·û±àÂëÀàÐÍ¡£ÀýÈ磺
<?xml version="1.0" encoding="us-ascii"?>
±àÂë˵Ã÷ÖеÄÄÚÈÝÒ²Ó¦¸ÃÈ·±£ÕýÈ·ÐÔ¡£XML ½âÎöÆ÷ʹÓñàÂëÈ·±£ XML Îĵµµ
Ïà¹ØÎĵµ£º
ʹÓÃJDOM½âÎöXML£¨×ªÔØ£¬¼òµ¥ÓÖÏêϸ£©
http://www.blogjava.net/haha/archive/2006/09/01/67153.html
Ò»¡¢Ç°ÑÔ
JDOMÊÇBreet MclaughlinºÍJason HunterÁ½´óJava¸ßÊֵĴ´×÷³É¹û£¬2000Äê³õ£¬JDOM×÷Ϊһ¸ö¿ª·ÅÔ´´úÂëÏîÄ¿Õýʽ¿ªÊ¼Ñз¢¡£JDOMÊÇÒ»ÖÖ½âÎöXMLµÄJava¹¤¾ß°ü¡£
&nbs ......
¶ÔÓÚ´øÓбí¿Õ¼äxmlnsµÄxmlÎļþµÄ½âÎö£¬ÓÃÕý³£½âÎöÎļþµÄ·½·¨×ÜÊÇʧЧ£¬²»Æð×÷Óã¬ÎÞ·¨»ñµÃÔªËØ¡£
ÏÂÃæ¸ø³öÁ½ÖÖ·½·¨½âÎö´ËÀàÎļþ£º
1.°´Õý³£½âÎöxmlÎļþµÄ·½·¨£¬ÐèҪעÒ⼸µã£º
»ñÈ¡ÔªËØElement£¬²»¿ÉʹÓú¯Êý£ºdocument.selectNodes("//region");
Ö»¿ÉÒÔÏÈÈ¡µ½¸ùÔªËØ£¬Ò»¼¶Ò»¼¶ÍùÏÂÈ¡£¬eg£º
Element root = document.g ......
Ò» DOM ÒÔÏ´úÂëÓÃDOMÊÇÉú³ÉÒ»¸öXMLÎĵµ
package xml;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
imp ......