SAXºÍDOM½âÎöXMLÎĵµµÄÇø±ð
ÔÚÕë¶ÔXMLÎĵµµÄÓ¦Óñà³Ì½Ó¿ÚÖУ¬×îÖ÷ÒªµÄÓÐW3CÖƶ¨µÄDOM(Document Object Method£¬Îĵµ¶ÔÏóÄ£ÐÍ)ºÍÓÉDavid MegginsonÁìµ¼µÄSAX(SimpleAPI for XML£¬ÓÃÓÚXMLµÄ¼òµ¥API)[19]¡£
SAXºÍDOMÔÚʵÏÖ¹ý³ÌÖУ¬·Ö±ð²àÖØÓÚ²»Í¬µÄ·½ÃæÒÔÂú×㲻ͬµÄÓ¦ÓÃÐèÇó¡£DOMΪ¿ª·¢»ùÓÚXMLµÄÓ¦ÓÃϵͳÌṩÁ˱ãÀû¡£Ëüͨ¹ýÒ»ÖÖËæ»ú·ÃÎÊ»úÖÆ£¬Ê¹µÃÓ¦ÓóÌÐòÀûÓøýӿڿÉÒÔÔÚÈκÎʱºò·ÃÎÊXMLÎĵµÖеÄÈκÎÒ»²¿·ÖÊý¾Ý£¬Ò²¿ÉÒÔ¶ÔXMLÎĵµÖеÄÊý¾Ý½øÐвåÈ롢ɾ³ý¡¢Ð޸ġ¢Òƶ¯µÈ²Ù×÷¡£ÔÚDOMÖУ¬ÎĵµµÄÂß¼½á¹¹ÀàËÆÒ»¿ÃÊ÷¡£Îĵµ¡¢ÎĵµÖеĸù¡¢ÔªËØ¡¢ÔªËØÄÚÈÝ¡¢ÊôÐÔ¡¢ÊôÐÔÖµµÈ¶¼ÊÇÒÔ¶ÔÏóÄ£Ð͵ÄÐÎʽ±íʾµÄ¡£DOMµÄÓŵãÔÚÓÚËüÔÚÄÚ´æÖб£´æÎĵµµÄÕû¸öÄ£ÐÍ¡£ÕâʹµÃÄÜÒÔÈκÎ˳Ðò·ÃÎÊXMLÔªËØ¡£È»¶ø£¬¶ÔÓÚ´óÐÍÎĵµÀ´Ëµ£¬ÕâÑù×ö¿ÉÄܲ»·½±ã¡£ÒòΪËü¿ÉÄÜ»áÓþ¡Äڴ棬»òÕßµ±ÏµÍ³´ïµ½ÁËËüµÄ¼«ÏÞʱ£¬»úÆ÷µÄÐÔÄܽ«
»áÂýÏÂÀ´¡£
SAXÌṩÁËÒ»ÖÖ¶ÔXMLÎĵµ½øÐÐ˳Ðò·ÃÎʵÄģʽ£¬ÕâÊÇÒ»ÖÖ¿ìËÙ¶ÁдXMLÊý¾ÝµÄ·½Ê½¡£SAX½Ó¿ÚÊÇʼþÇý¶¯µÄ£¬µ±Ê¹ÓÃSAX·ÖÎöÆ÷¶ÔXMLÎĵµ½øÐзÖÎöʱ£¬¾Í»á´¥·¢Ò»ÏµÁÐʼþ£¬²¢¼¤»îÏàÓ¦µÄʼþ´¦Àíº¯Êý£¬´Ó¶øÍê³É¶ÔXMLÎĵµµÄ·ÃÎÊ¡£SAX´¦ÀíXMLµÄ·½Ê½ÓëDOM²»Í¬¡£SAX½âÎöÆ÷²»Êǽ«DOMÊ÷½âÎöºÍ±íÏÖΪÊä³ö£¬ËüÊÇ»ùÓÚʼþµÄ£¬ËùÒÔÔÚXML±»½âÎöʱ£¬Ê¼þ±»·¢Ë͸øÒýÇæ¡£SAX¿ÉÒÔÔÚÎĵµµÄ¿ªÊ¼½ÓÊÕʼþ£¬Ò²¿ÉÒÔ½ÓÊÕÎĵµÖеÄÔªËØ¡£Ê¹ÓÃÕâЩʼþ¿ÉÒÔ¹¹½¨Ò»Öֽṹ¡£ÒòΪSAXûÓаÑXMLÎĵµÍêÈ«µØ¼ÓÔص½ÄÚ´æÖУ¬ËùÒÔÐèÒªµÄϵͳ×ÊÔ´½ÏÉÙ£¬ÊÇÒ»¸ö·ÖÎö´óÐÍXMLÎĵµµÄ¸ßЧAPI¡£È±µãÊDZàдSAX±È±àдDOM¸´ÔÓ£¬ÕâÒòΪÊ×ÏȱØÐëʵÏÖ֪ͨ½Ó¿Ú²¢Î¬»¤×´Ì¬£¬Æä´ÎSAX²»ÔÊÐí¶ÔÎĵµ½øÐÐËæ»ú·ÃÎÊ£¬Ò²Ã»ÓÐÌṩÏñDOMÄÇÑùµÄÐ޸ŦÄÜ¡£
±È½Ï¶øÑÔ£¬DOMºÍSAX¸÷ÓÐ×Ô¼ºµÄÓ¦Óó¡ºÏ¡£DOMÊÊÓÃÓÚ´¦ÀíÏÂÃæµÄÎÊÌ⣺½âÎö±È½ÏСµÄXMLÎļþ£»ÐèÒª¶ÔÎĵµ½øÐÐÐ޸ģ»ÐèÒªËæ»ú¶ÔÎĵµ½øÐзÃÎÊ¡£SAXÊÊÓÚ´¦ÀíÏÂÃæµÄÎÊÌ⣺¶Ô´óÐÍÎĵµ½øÐд¦Àí£»Ö»ÐèÒªÎĵµµÄ²¿·ÖÄÚÈÝ£»Ö»ÐèÒª´ÓÎĵµÖеõ½Ìض¨ÐÅÏ¢¡£
Ïà¹ØÎĵµ£º
XMLѧϰÍêÁË£¬ÀÏʦ˵ÔÚÃæÊÔµÄʱºòÓпÉÄÜÒªÎʵ½ÕâЩ£¬ËùÒÔÓÖÕÒÁËЩ×ÊÁÏ£¬ÔÚMLDNµÄÕâ¶ÎÈÕ×ÓÀÀÏʦÃǶ¼°Ñ¸÷¸öÃæÊÔÖÐÓпÉÄÜÒªÎʵ½µÄÎÊÌⶼ°ïÎÒÃÇÒ»Ò»×ܽᣬÕæµÄºÜ¸ÐлMLDNµÄÈ«²¿ÀÏʦ,ÏîÄ¿ÀÏʦ°ïÎÒÃÇÈÏÕæµÄ½²½âÏîÄ¿£¬¸¨µ¼ÏîÄ¿£¬½ÌѧÀÏʦ°ïÎÒÃÇÈÏÕæ×ܽá¸÷¸ö¼¼Êõ£¬¸Ðл£¬ÕæµÄÊǸÐл£¬ºÇºÇ£¬ÎÒ»áŬÁ¦¸ãºÃµÄ
JavaÖÐËÄÖÖXML ......
import java.io.File;
import java.io.FileNotFoundException;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class ResolveXmlFile {
public void resolverXml() throws DocumentException, FileNotFoundExcept ......
×î³£¼ûµÄXMLÊý¾ÝÀàÐÍÓУºElement, Attribute£¬Comment, Text.
Element, Ö¸ÐÎÈç<Name>Tom<Name>µÄ½Úµã¡£Ëü¿ÉÒÔ°üÀ¨£ºElement, Text, Comment, ProcessingInstruction, CDATA, and EntityReference.
Attribute, Ö¸ÔÚ<Employee >ÖеĴ ......
package com.jcauto.action;
import java.util.ArrayList;
import java.util.List;
public class ContentRsp {
private String resultCode;
List<ContentInfo> contentList = new ArrayList<ContentInfo>();
public void addContent(ContentInfo contentInfo) {
contentList.add(contentI ......