無為

          無為則可為,無為則至深!

            BlogJava :: 首頁 :: 聯系 :: 聚合  :: 管理
            190 Posts :: 291 Stories :: 258 Comments :: 0 Trackbacks

          編譯環境是jdk1.5+xerces-j-2.8
          源碼為:import java.io.*;
          import org.xml.sax.*;
          import org.xml.sax.helpers.XMLReaderFactory;
          import org.apache.xerces.parsers.*;
          ?public class ParseXMLDTD
          {
          ?? ?public void ShowParseResult(String uri)
          ?? ?{
          ?? ??? ?System.out.println("Parse"+"uri"+"File!");
          ?? ??? ?System.out.println("===============================");
          ?? ??? ?ContentHandler Chandler=new ChandlerClass1();
          ?? ??? ?ErrorHandler EHandler=new EHandlerClass1();
          ?? ??? ?DTDHandler DHandler=new DTDHandlerClass1();
          ?? ??? ?try
          ?? ??? ?{
          ?? ??? ??? ?XMLReader MyParser=XMLReaderFactory.createXMLReader("org.apache.xerces.parsers.SAXParser");
          ?? ??? ??? ?MyParser.setContentHandler(Chandler);
          ?? ??? ??? ?MyParser.setErrorHandler(EHandler);
          ?? ??? ??? ?MyParser.setDTDHandler(DHandler);
          ?? ??? ??? ?MyParser.setFeature("http://xml.org/sax/features/validation",true);
          ?? ??? ??? ?MyParser.parse(uri);
          ?? ??? ?}
          ?? ??? ?catch(IOException e)
          ?? ??? ?{
          ?? ??? ??? ?System.out.println("Error IOException !!!");
          ?? ??? ?}
          ?? ??? ?catch(SAXException e)
          ?? ??? ?{
          ?? ??? ??? ?System.out.println("Error SAXException !!!");
          ?? ??? ?}
          ?? ??? ?
          ?? ?}
          ?? ?public static void main(String args[])
          ?? ?{
          ?? ??? ?String uri=args[0];
          ?? ??? ?ParseXMLErrorLog ParseXMLErrorLogInstance=new ParseXMLErrorLog();
          ?? ??? ?ParseXMLErrorLogInstance.ShowParseResult(uri);
          ?? ?}
          }
          class ChandlerClass1 implements ContentHandler
          {
          ?? ?private Locator locator;
          ?? ?public void setDocumentLocator(Locator locator)
          ?? ?{
          ?? ??? ?System.out.println("The Parser called setDocumentLocator()!!!");
          ?? ??? ?this.locator=locator;
          ?? ??? ?System.out.println("===============================");
          ?? ?}
          ?? ?public void startDocument() throws SAXException
          ?? ?{
          ?? ??? ?System.out.println("The Parser called startDocument()!!!");
          ?? ??? ?System.out.println("===============================");
          ?? ?}
          ?? ?public void endDocument() throws SAXException
          ?? ?{
          ?? ??? ?System.out.println("The Parser called endDocument()!!!");
          ?? ??? ?System.out.println("===============================");
          ?? ?}
          ?? ?public void processingInstruction(String target,String data)
          ?? ?{
          ?? ??? ?System.out.println("The Parser called processingInstruction()!!!");
          ?? ??? ?System.out.println("target is {"+target+"}"+"data is {"+data+"}");
          ?? ??? ?System.out.println("===============================");
          ?? ?}
          ?? ?public void startPrefixMapping(String prefix,String uri)
          ?? ?{
          ?? ??? ?System.out.println("The Parser called startPrefixMapping()!!!");
          ?? ??? ?System.out.println("prefix is {"+prefix+"}uri is {"+uri+"}");
          ?? ??? ?System.out.println("===============================");
          ?? ?}
          ?? ?public void endPrefixMapping(String prefix)
          ?? ?{
          ?? ??? ?System.out.println("The Parser called endPrefixMapping()!!!");
          ?? ??? ?System.out.println("prefix is {"+prefix+"}");
          ?? ??? ?System.out.println("===============================");
          ?? ?}
          ?? ?public void startElement(String namespaceURL,String localName,String rawName,Attributes atts) throws SAXException
          ?? ?{
          ?? ??? ?System.out.println("The Parser called startElement()!!!");
          ?? ??? ?if(!namespaceURL.equals(""))
          ?? ??? ?{
          ?? ??? ??? ?System.out.println("nameSpaceURL is {"+namespaceURL+"}"+"rawName is {"+rawName+"}");
          ?? ??? ?}
          ?? ??? ?else
          ?? ??? ?{
          ?? ??? ??? ?System.out.println("There is not namespace");
          ?? ??? ?}
          ?? ??? ?System.out.println("localName is {"+localName+"}");
          ?? ??? ?for(int i=0;i<atts.getLength();i++)
          ?? ??? ?{
          ?? ??? ??? ?System.out.println("Attribute is {"+atts.getLocalName(i)+"}is {"+atts.getValue(i)+"}");
          ?? ??? ?}
          ?? ??? ?System.out.println("===============================");
          ?? ?}
          ?? ?public void endElement(String namespaceURL,String localName,String rawName) throws SAXException
          ?? ?{
          ?? ??? ?System.out.println("The Parser called endElement()!!!");
          ?? ??? ?System.out.println("localName is {"+localName+"}");
          ?? ??? ?System.out.println("===============================");
          ?? ?}
          ?? ?public void characters(char[] ch,int start,int end)
          ?? ?{
          ?? ??? ?String sss=new String(ch,start,end);
          ?? ??? ?System.out.println("The Parser called characters()!!!");
          ?? ??? ?System.out.println("characters is {"+sss+"}");
          ?? ??? ?System.out.println("===============================");
          ?? ?}
          ?? ?public void ignorableWhitespace(char[] ch,int start,int end) throws SAXException
          ?? ?{
          ?? ??? ?String sss=new String(ch,start,end);
          ?? ??? ?System.out.println("The Parser called ignorableWhitespace()!!!");
          ?? ??? ?System.out.println("ignorableWhitespace is {"+sss+"}");
          ?? ??? ?System.out.println("===============================");
          ? }
          ? public void skippedEntity(String name) throws SAXException
          ? {
          ? ?? ?System.out.println("The Parser called skippedEntity()!!!");
          ?? ??? ?System.out.println("skippedEntity {"+name+"}");
          ?? ??? ?System.out.println("===============================");
          ? }
          }
          class EHandlerClass1 implements ErrorHandler
          {
          ?? ?public void warning(SAXParseException spe) throws SAXException
          ?? ?{
          ?? ??? ?System.out.println("The Parser called warning()!!!!");
          ?? ??? ?System.out.println("Line is "+spe.getLineNumber());
          ?? ??? ?System.out.println("URI is "+spe.getSystemId());
          ?? ??? ?System.out.println("Message is "+spe.getMessage());
          ?? ??? ?System.out.println("===============================");
          ?? ?}
          ?? ?public void error(SAXParseException spe) throws SAXException
          ?? ?{
          ?? ??? ?System.out.println("The Parser called error()!!!!");
          ?? ??? ?System.out.println("Line is "+spe.getLineNumber());
          ?? ??? ?System.out.println("URI is "+spe.getSystemId());
          ?? ??? ?System.out.println("Message is "+spe.getMessage());
          ?? ??? ?System.out.println("===============================");
          ?? ?}
          ?? ?public void fatalError(SAXParseException spe) throws SAXException
          ?? ?{
          ?? ??? ?System.out.println("The Parser called fatalError()!!!!");
          ?? ??? ?System.out.println("Line is "+spe.getLineNumber());
          ?? ??? ?System.out.println("URI is "+spe.getSystemId());
          ?? ??? ?System.out.println("Message is "+spe.getMessage());
          ?? ??? ?System.out.println("===============================");
          ?? ?}
          }

          class DTDHandlerClass1 implements DTDHandler //聲明DTDHandlerClass,此類實現DTDHandler接口
          {
          ?? ?public void notationDecl(String? name,String publicId,String systemId) throws SAXException
          ?? ?{
          ?? ??? ?//輸出符號的相關信息
          ?? ??? ?System.out.println("The Parser called notationDecl()!!!!");
          ?? ??? ?System.out.println("name is "+name);
          ?? ??? ?System.out.println("publicId is "+publicId);
          ?? ??? ?System.out.println("systemId is "+systemId);
          ?? ??? ?System.out.println("===============================");
          ?? ?}
          ?? ?public void unparsedEntityDecl(String name,String publicId,String systemId,String notationName) throws SAXException
          ?? ?{
          ?? ??? ?//輸出不可分析實體的相關信息
          ?? ??? ?System.out.println("The Parser called unparsedEntityDecl()!!!!");
          ?? ??? ?System.out.println("name is "+name);
          ?? ??? ?System.out.println("publicId is "+publicId);
          ?? ??? ?System.out.println("systemId is "+systemId);
          ?? ??? ?System.out.println("notationName is "+notationName);
          ?? ??? ?System.out.println("===============================");
          ?? ?}
          }

          采用的xml文件為:second.xml
          <?xml version="1.0" encoding="gb2312"?>
          <!DOCTYPE employee_data [
          <!ELEMENT employee_inf (employee_data+)>
          <!ELEMENT employee_data (name,age,sex,address,E_mail,photo)>
          <!ELEMENT name (#PCDATA)>
          <!ELEMENT age (#PCDATA)>
          <!ELEMENT sex (#PCDATA)>
          <!ELEMENT address (#PCDATA)>
          <!ELEMENT E_mail (#PCDATA)>
          <!ELEMENT Photo EMPTY>
          <!NOTATION jpg SYSTEM "C:\windows\system32\mspaint.exe">
          <!ENTITY mm_photo SYSTEM "mm_photo.jpg" NDATA jpg>
          <!ATTLIST photo picture ENTITY #IMPLIED>
          <!ATTLIST photo sw_tool NOTATION (jpg) #IMPLIED>
          ]>
          <employee_inf>
          ?? <employee_data>
          ????? <name>馬連浩</name>
          ????? <age>24</age>
          ????? <sex>男</sex>
          ????? <address>大連交通大學</address>
          ????? <E_mail> mlh123caoer@tom.com</E_mail>
          ????? <photo picture="mm_photo" sw_tool="jpg"/>
          ?? </employee_data>
          </employee_inf>
          dos輸出效果為:
          D:\2000>javac ParseXMLDTD.java

          D:\2000>java ParseXMLDTD second.xml
          ParseuriFile!
          ===============================
          The Parser called setDocumentLocator()!!!
          ===============================
          The Parser called startDocument()!!!
          ===============================
          The Parser called notationDecl()!!!!
          name is jpg
          publicId is null
          systemId is file:///C:/windows/system32/mspa
          ===============================
          The Parser called unparsedEntityDecl()!!!!
          name is mm_photo
          publicId is null
          systemId is file:///D:/2000/mm_photo.jpg
          notationName is jpg
          ===============================
          The Parser called startElement()!!!
          There is not namespace
          localName is {employee_inf}
          ===============================
          The Parser called ignorableWhitespace()!!!
          ignorableWhitespace is {
          ?? }
          ===============================
          The Parser called startElement()!!!
          There is not namespace
          localName is {employee_data}
          ===============================
          The Parser called ignorableWhitespace()!!!
          ignorableWhitespace is {
          ????? }
          ===============================
          The Parser called startElement()!!!
          There is not namespace
          localName is {name}
          ===============================
          The Parser called characters()!!!
          characters is {馬連浩}
          ===============================
          The Parser called endElement()!!!
          localName is {name}
          ===============================
          The Parser called ignorableWhitespace()!!!
          ignorableWhitespace is {
          ????? }
          ===============================
          The Parser called startElement()!!!
          There is not namespace
          localName is {age}
          ===============================
          The Parser called characters()!!!
          characters is {24}
          ===============================
          The Parser called endElement()!!!
          localName is {age}
          ===============================
          The Parser called ignorableWhitespace()!!!
          ignorableWhitespace is {
          ????? }
          ===============================
          The Parser called startElement()!!!
          There is not namespace
          localName is {sex}
          ===============================
          The Parser called characters()!!!
          characters is {男}
          ===============================
          The Parser called endElement()!!!
          localName is {sex}
          ===============================
          The Parser called ignorableWhitespace()!!!
          ignorableWhitespace is {
          ????? }
          ===============================
          The Parser called startElement()!!!
          There is not namespace
          localName is {address}
          ===============================
          The Parser called characters()!!!
          characters is {大連交通大學}
          ===============================
          The Parser called endElement()!!!
          localName is {address}
          ===============================
          The Parser called ignorableWhitespace()!!!
          ignorableWhitespace is {
          ????? }
          ===============================
          The Parser called startElement()!!!
          There is not namespace
          localName is {E_mail}
          ===============================
          The Parser called characters()!!!
          characters is { mlh123caoer@tom.com}
          ===============================
          The Parser called endElement()!!!
          localName is {E_mail}
          ===============================
          The Parser called ignorableWhitespace()!!!
          ignorableWhitespace is {
          ????? }
          ===============================
          The Parser called startElement()!!!
          There is not namespace
          localName is {photo}
          Attribute is {picture}is {mm_photo}
          Attribute is {sw_tool}is {jpg}

          ===============================
          The Parser called endElement()!!!
          localName is {photo}
          ===============================
          The Parser called ignorableWhitespace()!!!
          ignorableWhitespace is {
          ?? }
          ===============================
          The Parser called endElement()!!!
          localName is {employee_data}
          ===============================
          The Parser called ignorableWhitespace()!!!
          ignorableWhitespace is {
          }
          ===============================
          The Parser called endElement()!!!
          localName is {employee_inf}
          ===============================
          The Parser called endDocument()!!!
          ===============================
          設計者:mlhcaoer 郵箱:mlh123caoer@tom.com QQ:215960358
          歡迎高手指導


          凡是有該標志的文章,都是該blog博主Caoer(草兒)原創,凡是索引、收藏
          、轉載請注明來處和原文作者。非常感謝。

          posted on 2006-04-20 08:45 草兒 閱讀(483) 評論(0)  編輯  收藏 所屬分類: 軟件構架
          主站蜘蛛池模板: 贵德县| 通州区| 湄潭县| 巫溪县| 泰宁县| 银川市| 蒙自县| 凌源市| 吉首市| 江达县| 聂拉木县| 东城区| 西吉县| 历史| 东丽区| 外汇| 和平县| 小金县| 方城县| 大庆市| 河间市| 伊宁县| 湛江市| 太湖县| 河西区| 宁南县| 陈巴尔虎旗| 建始县| 禄劝| 阜宁县| 镇巴县| 柳河县| 会同县| 镇沅| 辽宁省| 神农架林区| 张家港市| 辰溪县| 华容县| 洪雅县| 自治县|