论坛首页 入门技术论坛

XML 文件的操作(四)

浏览 1462 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2009-01-08  
第一个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();
		}
	}
}
论坛首页 入门技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics