---
title: "指定使用特定的xml解析器"
author: "solomon"
date: "2010-12-08T17:26:06Z"
tags: []
categories: ["非技术"]
slug: "xml"
draft: false
lastmod: "2010-12-08T17:26:06Z"
id: 836889
url: "https://solomon.iteye.com/blog/836889"
---

#### 指定xml解析器

     阅读JDK源码javax.xml.parsers.FactoryFinder，javax.xml.parsers.SAXParserFactory以及DocumentBuilderFactory可以发现JDK按照如下顺序来寻找XML解析器：

1\. 系统属性javax.xml.parsers.DocumentBuilderFactory或javax.xml.parsers.SAXParserFactory

2\. 在jdk-dir/lib/jaxp.properties中设定的javax.xml.parsers.DocumentBuilderFactory或     javax.xml.parsers.SAXParserFactory属性

3\. 运行时jar包中META-INF/services/javax.xml.parsers.DocumentBuilderFactory或 javax.xml.parsers.SAXParserFactory文件中设定的值

4\. 如果上面的解析器都没有找到，则使用Crimson。如果还没有那只能ClassNotFound了。

     这样，我们可以通过调用 System.setProperty("javax.xml.parsers.DocumentBuilderFactory","     org.apache.crimson.jaxp.DocumentBuilderFactoryImpl")来设定相应的XML解析器，或者生成 jaxp.properties文件，在其中加入如下内容

    javax.xml.parsers.DocumentBuilderFactory = org.apache.crimson.jaxp.DocumentBuilderFactoryImpl  
或者在打jar包的时候加上文件名为javax.xml.parsers.DocumentBuilderFactory的文件，然后再其中写org.apache.crimson.jaxp.DocumentBuilderFactoryImpl