java.lang.IllegalStateException: can't declare any more prefixes in this context

来源:互联网 发布:淘宝下班后回复话术 编辑:程序博客网 时间:2024/05/23 20:32
jdom/dom4j在解释xml时,用的是ibm的jre 1.4.1时,如果xml文件中有xmlns:udm时,就会在将string input stream转成document时出现:java.lang.IllegalStateException: can't declare any more prefixes in this context错误,请教各位大虾这问题怎么解决?,

xml文件中包含一个这样的元素:
<element5 xmlns:xmi="http://www.omg.org/XMI" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
如果用sun的jdk1.4.2就没这个问题.
异常详细信息如下 :
Root cause: java.lang.IllegalStateException: can't declare any more prefixes in this context
at org.apache.crimson.parser.Parser2.parseInternal(Parser2.java:691)
at org.apache.crimson.parser.Parser2.parse(Parser2.java:337)
at org.apache.crimson.parser.XMLReaderImpl.parse(XMLReaderImpl.java:448)
at org.jdom.input.SAXBuilder.build(SAXBuilder.java:287)
at org.jdom.input.SAXBuilder.build(SAXBuilder.java:583)

原因分析:

是由于xml parser: Crimson不能解释带有类似于xmlns:xsi这样的属性.要用Xerces才行,但是IBM的jre在默认情况下寻找Crimson作为parser解决办法是通过在程序中设定jre的xml parser为Xerces,同时将Xerces的jar包加入到classpath中,一共有三个包:xalan.jar,xercesImpl.jar,xml-apis.jar

从http://xml.apache.org/xalan-j/上可以下载.

http://xerces.apache.org/xerces-j/
jre的xml parser设置:是jrelib下的一个叫jaxp.properties文件,最后有三行是关于指定 xml parser的,将注释去掉,或者在程序中加入以下语句也可以:
System.setProperty("javax.xml.parsers.DocumentBuilderFactory","org.apache.xerces.jaxp.DocumentBuilderFactoryImpl");
System.setProperty("javax.xml.parsers.SAXParserFactory","org.apache.xerces.jaxp.SAXParserFactoryImpl");

如下链接中的文章解释得非常具体:
http://www.javaworld.com.tw/jute/post/view?bid=19&id=14958&sty=3

 

 

/IBM/WebSphere/AppServer/java/jre/lib/

目录下增加 三个架包

xercesImpl.jar

xalan.jar

xml-apis.jar

建立文件jaxp.properties

内容如下:

javax.xml.transform.TransformerFactory=org.apache.xalan.processor.TransformerFactoryImpl
javax.xml.parsers.SAXParserFactory=org.apache.xerces.jaxp.SAXParserFactoryImpl
javax.xml.parsers.DocumentBuilderFactory=org.apache.xerces.jaxp.DocumentBuilderFactoryImpl 

 

原创粉丝点击