无为

无为则可为,无为则至深!

  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 草儿 阅读(464) 评论(0)  编辑  收藏 所属分类: 软件构架

只有注册用户登录后才能发表评论。


网站导航: