XML 文件的操作(四)


Dtd代码 复制代码
  1. <!NOTATION gif SYSTEM "image/gif">   
  2. <!NOTATION jpg SYSTEM "iexplore.exe">   
  3.   
  4. <!ENTITY logo SYSTEM "http://www.sunxin.org/logo.gif" NDATA gif>   
  5. <!ENTITY banner SYSTEM "http://www.sunxin.org/banner.gif" NDATA jpg>  

第二个dtd文件,包含一个dtd文件:
Dtd代码 复制代码
  1. <!ELEMENT hr (#PCDATA)>   
  2. <!ENTITY % entitiesDecl SYSTEM "entity.dtd">   
  3. %entitiesDecl;  

xml文件,包含第二个dtd文件:
Xml代码 复制代码
  1. package com.ibm.xml;   
  2.   
  3. import java.io.File;   
  4. import java.io.IOException;   
  5.   
  6. import javax.xml.parsers.DocumentBuilder;   
  7. import javax.xml.parsers.DocumentBuilderFactory;   
  8. import javax.xml.parsers.ParserConfigurationException;   
  9.   
  10. import org.w3c.dom.Document;   
  11. import org.w3c.dom.DocumentType;   
  12. import org.w3c.dom.Entity;   
  13. import org.w3c.dom.NamedNodeMap;   
  14. import org.w3c.dom.Notation;   
  15. import org.xml.sax.SAXException;   
  16.   
  17. /**   
  18.  * 获取xml所有元素,连接dtd文件   
  19.  * @author Administrator   
  20.  *   
  21.  */   
  22. public class DocTypePrinter {   
  23.   
  24.     public static void main(String arge[]){   
  25.         DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();   
  26.         try{   
  27.             //读取文件   
  28.             DocumentBuilder db=dbf.newDocumentBuilder();   
  29.             Document doc=db.parse(new File("hr.xml"));   
  30.             DocumentType docdocType=doc.getDoctype();   
  31.             if(docType!=null)   
  32.             {   
  33.                 //读取xml文件内的内容   
  34.                 //xml dtd名称   
  35.                 System.out.println("dtd name: "+docType.getName());   
  36.                 //xml public id   
  37.                 System.out.println("dtd public id: "+docType.getPublicId());   
  38.                 //xml 系统标示信息   
  39.                 System.out.println("dtd system id: "+docType.getSystemId());   
  40.                 //xml 获取引用信息   
  41.                 System.out.println("dtd internal subset: "+docType.getInternalSubset());   
  42.                 System.out.println();   
  43.                 //获取实体内容   
  44.                 NamedNodeMap entities=docType.getEntities();   
  45.                 //节点数目   
  46.                 int len=entities.getLength();   
  47.                 for(int i=0;i<len;i++)   
  48.                 {   
  49.                     //获取实体   
  50.                     Entity entity=(Entity)entities.item(i);   
  51.                     //获取实体名称   
  52.                     System.out.println("entity name: "+entity.getNodeName());   
  53.                     //获取外部实体名称   
  54.                     System.out.println("notation name: "+entity.getNotationName());   
  55.                     //获取实体公共标示   
  56.                     System.out.println("entity public id: "+entity.getPublicId());   
  57.                     //获取实体系统标示   
  58.                     System.out.println("entity system id: "+entity.getSystemId());   
  59.                     System.out.println();   
  60.                 }   
  61.                 //获取记号,映射   
  62.                 NamedNodeMap notations=docType.getNotations();   
  63.                 //节点数目   
  64.                 len=notations.getLength();   
  65.                 for(int i=0;i<len;i++)   
  66.                 {   
  67.                     //获取映射   
  68.                     Notation notation=(Notation)notations.item(i);   
  69.                     //映射名称   
  70.                     System.out.println("notation name: "+notation.getNodeName());   
  71.                     //映射公共标示   
  72.                     System.out.println("notation public id: "+notation.getPublicId());   
  73.                     //映射系统标示   
  74.                     System.out.println("notation system id: "+notation.getSystemId());   
  75.                        
  76.                     System.out.println();   
  77.                 }   
  78.             }   
  79.         }catch (ParserConfigurationException e)   
  80.         {   
  81.             // TODO 自动生成 catch 块   
  82.             e.printStackTrace();   
  83.         }   
  84.         catch (SAXException e)   
  85.         {   
  86.             // TODO 自动生成 catch 块   
  87.             e.printStackTrace();   
  88.         }   
  89.         catch (IOException e)   
  90.         {   
  91.             // TODO 自动生成 catch 块   
  92.             e.printStackTrace();   
  93.         }   
  94.     }   
  95. }  
posted @ 2009-01-08 16:37  猪鼻驴耳  阅读(151)  评论(0)    收藏  举报