DOM 与 DOM 解析详解(二)关于 DOMImplementation

关于DOMImplementation 示例讲解(讲解请见注释)

 

package Test_DOM;

import org.w3c.dom.DOMImplementation;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.ParserConfigurationException;

import static java.lang.System.out;

public class Test {

	public static void main(String[] args) throws ParserConfigurationException {
		DocumentBuilderFactory builderFactory = DocumentBuilderFactory
				.newInstance();
		DocumentBuilder builder = builderFactory.newDocumentBuilder();
		
		/*
		 * DOMImplementation 接口为执行独立于文档对象模型的任何特定实例的操作提供了许多方法。
		 * 如 hasFeature() 方法和 getFeature() 方法。
		 */
		DOMImplementation domImplementation = builder.getDOMImplementation();
		String[][] features = new String[][] { 
				{ "XML", "1.0" },
				{ "XHTML", "5.0" }, 
				{ "XPATH", "1.0" }};
		for (String[] feature : features) {
			/*
			 * hasFeature()方法 测试 DOM 实现是否实现在其中指定的特定特性和版本。
			 */
			boolean hasFeature = domImplementation.hasFeature(feature[0], feature[1]);
			out.println("hasFeature("+feature[0]+","+feature[1]+") = " + hasFeature);
			if(hasFeature){
				/*
				 * getFeature()此方法返回一个特定的对象,该对象实现了所指定的特定特性或版本的特殊 API。
				 */
				Object featureObj = domImplementation.getFeature(feature[0], feature[1]);
				out.println("  featureObject's class is "+featureObj.getClass().getName());
			}
		}
	}

}
 

运行输出如下:


  hasFeature(XML,1.0) = true
    featureObject's class is com.sun.org.apache.xerces.internal.dom.CoreDOMImplementationImpl
  hasFeature(XHTML,5.0) = false
  hasFeature(XPATH,1.0) = false

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值