jaxp解析DTD的例子

相信想知道jaxp包如何进行DTD解析的人,看了这个简单例子就明白了。
1
:类DTDTest源程序;
2
:用于测试的DTD文档:boolk-order.dtd;
3
:程序输出结果。

至于包,可从sun的网站得到。
1:DTDTest.java:

importorg.xml.sax.InputSource;
importcom.sun.xml.parser.Parser;
importjava.io.*;
importorg.xml.sax.*;
importcom.sun.xml.parser.DtdEventListener;
publicclass DTDTest implements DtdEventListener{
private Parser parser;
private String dtdSource;
private InputSource buf;
public DTDtest(){
parser=new Parser();
parser.setDTDHandler(this);
File f=newFile("file:book-order.dtd");
dtdSource=f.getPath();
byte[]data=("<!DOCTYPE UNKNowN SYstem\""+dtdSource+"\"> <x />").getBytes();
buf=newInputSource(new ByteArrayInputStream(data));
Stringparent=f.getParent();
if(parent==null)parent=".\\";
buf.setSystemId(parent);
}
public void parse() throws Exception{
parser.parse(buf);
}
public static void main(String args[]){
DTDTest dtdtest=newDTDtest();
try {dtdtest.parse();}
catch (Exception e){}
}

/**Thefollowing will implement methods in DtdEventListener interface******/
public void notationDecl(String name,StringpublicId,String systemId){}

public void unparsedEntityDecl(Stringname,String publicId,String systemId,String notationName){}
public void startDtd (String rootName) throwsSAXException{
System.out.println("Now begin to start to parse DTD File!");
if (rootName!=null)System.out.println("rootName: "+rootName);
System.out.println("");
}
public void externalDtdDecl (String publicId,String systemId) throws SAXException{
System.out.println("Now begin to do externalDtdDecl!");
if (publicId!=null)System.out.println("publicId: "+publicId);
if (systemId!=null)System.out.println("syutemId: "+systemId);
System.out.println("");
}
public void internalDtdDecl (StringinternalSubset) throws SAXException{
System.out.println("Nowshow internalDtdDecl!");
if(internalSubset!=null) System.out.println("internalSubset:"+internalSubset);
System.out.println("");
}
public void internalEntityDecl (String name,String value) throws SAXException
{
System.out.println("Nowshow internalEntityDecl!");
if(name!=null) System.out.println("name: "+name);
if (value!=null)System.out.println("value: "+value);
System.out.println("");
}
public void externalEntityDecl (Stringname,String systemId) throws SAXException{
System.out.println("Nowshow externalEntityDecl!");
if(name!=null) System.out.println("name: "+name);
if(publicId!=null) System.out.println("publicId: "+publicId);
if(systemId!=null) System.out.println("systemId: "+systemId);
System.out.println("");
}
public void elementDecl (String elementName,String contentModel) throws SAXException{
System.out.println("Nowshow elementDecl!");
if(elementName!=null) System.out.println("elementName: "+elementName);
if(contentModel!=null) System.out.println("contentModel:"+contentModel);
System.out.println("");
}
public void attributeDecl (StringelementName,StringattributeName,StringattributeType,String options [],StringdefaultValue,Boolean isFixed,booleanisrequired)throws SAXException{
System.out.println("Now show attributeDecl!");
if(elementName!=null) System.out.println("elementName: "+elementName);
if (attributeName!=null)System.out.println("attributeName:"+attributeName);
if (attributeType!=null)System.out.println("attributeType:"+attributeType);
if (defaultValue!=null)System.out.println("defaultValue:"+defaultValue);
if (options!=null){
System.out.println("attribute value options:");
for (int i=0;i<options.length;i++)System.out.println(options[i]);
}
System.out.println("");
}
public void endDtd () throws SAXException{
System.out.println("parse DTDFile is end!");
System.out.println("bye-bye");
}
}


2:book-order.dtd:

<!ELEMENT Order (Customer,Manifest,Receipt)>
<!ATTLIST Order xmlns CDATA #FIXED"http://www.example.com/myschema.xml">
<!ELEMENT Customer (Name,Cardnum)>
<!ELEMENT Name (#PCDATA)>
<!ELEMENT Cardnum (#PCDATA)>
<!ELEMENT Manifest (Item*)>
<!ELEMENT Item (ID,Title,Quantity,UnitPrice)>
<!ELEMENT ID (#PCDATA)>
<!ELEMENT Title (#PCDATA)>
<!ELEMENT Quantity (#PCDATA)>
<!ELEMENT UnitPrice (#PCDATA)>
<!ELEMENT Receipt (Subtotal,Tax,Total)>
<!ELEMENT Subtotal (#PCDATA)>
<!ELEMENT Tax (#PCDATA)>
<!ELEMENT Total (#PCDATA)>

3: 程序输出结果: Nowbegin to start to parse DTD File! rootName:UNKNowNNow begin to doexternalDtdDecl! syutemId:file:book-order.dtdNow show elementDecl! elementName:Order contentModel:(Customer,Receipt)Now show attributeDecl! elementName: Order attributeName: xmlns attributeType: CDATA defaultValue:http://www.example.com/myschema.xmlNow showelementDecl! elementName:Customer contentModel:(Name,Cardnum)Now showelementDecl! elementName: Name contentModel:(#PCDATA)Now show elementDecl! elementName:Cardnum contentModel:(#PCDATA)Now show elementDecl! elementName:Manifest contentModel:(Item*)Nowshow elementDecl! elementName: Item contentModel:(ID,UnitPrice)Nowshow elementDecl! elementName: ID contentModel: (#PCDATA)Now show elementDecl! elementName:Title contentModel: (#PCDATA)Now showelementDecl! elementName:Quantity contentModel:(#PCDATA)Now showelementDecl! elementName:UnitPrice contentModel: (#PCDATA)Now show elementDecl! elementName:Receipt contentModel: (Subtotal,Total)Now show elementDecl!elementName: SubtotalcontentModel:(#PCDATA)Now show elementDecl! elementName:TaxcontentModel: (#PCDATA)Now showelementDecl! elementName:Total contentModel: (#PCDATA) parseDTDFile is end! bye-bye ProcessExit...

相关文章

php输出xml格式字符串
J2ME Mobile 3D入门教程系列文章之一
XML轻松学习手册
XML入门的常见问题(一)
XML入门的常见问题(三)
XML轻松学习手册(2)XML概念