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

 

 

 

 

 

 

你可能感兴趣的:(apache,xml,XHTML,sun)