XML 文件的操作(四)

第一个dtd文件:

<!NOTATION gif SYSTEM "image/gif">
<!NOTATION jpg SYSTEM "iexplore.exe">

<!ENTITY logo SYSTEM "http://www.sunxin.org/logo.gif" NDATA gif>
<!ENTITY banner SYSTEM "http://www.sunxin.org/banner.gif" NDATA jpg>

第二个dtd文件,包含一个dtd文件:

<!ELEMENT hr (#PCDATA)>
<!ENTITY % entitiesDecl SYSTEM "entity.dtd">
%entitiesDecl;

xml文件,包含第二个dtd文件:

package com.ibm.xml;

import java.io.File;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.DocumentType;
import org.w3c.dom.Entity;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Notation;
import org.xml.sax.SAXException;

/**
* 获取xml所有元素,连接dtd文件
* @author Administrator
*
*/
public class DocTypePrinter {

public static void main(String arge[]){
DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
try{
//读取文件
DocumentBuilder db=dbf.newDocumentBuilder();
Document doc=db.parse(new File("hr.xml"));
DocumentType docType=doc.getDoctype();
if(docType!=null)
{
//读取xml文件内的内容
//xml dtd名称
System.out.println("dtd name: "+docType.getName());
//xml public id
System.out.println("dtd public id: "+docType.getPublicId());
//xml 系统标示信息
System.out.println("dtd system id: "+docType.getSystemId());
//xml 获取引用信息
System.out.println("dtd internal subset: "+docType.getInternalSubset());
System.out.println();
//获取实体内容
NamedNodeMap entities=docType.getEntities();
//节点数目
int len=entities.getLength();
for(int i=0;i<len;i++)
{
//获取实体
Entity entity=(Entity)entities.item(i);
//获取实体名称
System.out.println("entity name: "+entity.getNodeName());
//获取外部实体名称
System.out.println("notation name: "+entity.getNotationName());
//获取实体公共标示
System.out.println("entity public id: "+entity.getPublicId());
//获取实体系统标示
System.out.println("entity system id: "+entity.getSystemId());
System.out.println();
}
//获取记号,映射
NamedNodeMap notations=docType.getNotations();
//节点数目
len=notations.getLength();
for(int i=0;i<len;i++)
{
//获取映射
Notation notation=(Notation)notations.item(i);
//映射名称
System.out.println("notation name: "+notation.getNodeName());
//映射公共标示
System.out.println("notation public id: "+notation.getPublicId());
//映射系统标示
System.out.println("notation system id: "+notation.getSystemId());

System.out.println();
}
}
}catch (ParserConfigurationException e)
{
// TODO 自动生成 catch 块
e.printStackTrace();
}
catch (SAXException e)
{
// TODO 自动生成 catch 块
e.printStackTrace();
}
catch (IOException e)
{
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值