FWK005 parse may not be called while parsing
http://xieshaohu.wordpress.com/2011/04/21/fwk005-parse-may-not-be-called-while-parsing/
FWK005 parse may not be called while?parsing.
最近在使用javax.xml.parsers.DocumentBuilder解析xml文件的時候偶爾會出錯:
org.xml.sax.SAXException: FWK005 parse may not be called while parsing.at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:263)at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:284)at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:208) ...跟蹤了一下代碼,發現這個異常是在com.sun.org.apache.xerces.internal.parsers.DTDConfiguration.parse(DTDConfiguration.java:546)拋出來的。該段代碼結構如下:
if(fParseInProgress) {throw new XNIException("FWK005 parse may not be called while parsing."); }fParseInProgress = true;// 解析xml文件finally {fParseInProgress = false; }從程序邏輯來看,如果當前DocumentBuilder對象正在轉換文檔,此時再次請求轉換文檔,那么直接拋出XNIException(“FWK005 parse may not be called while parsing.”);異常。
這個問題也比較好解決,一種是對轉換xml文檔的方法,增加synchronized關鍵字,這樣子不會有兩個線程同時訪問方法。
還有一種方法是創建一個DocumentBuilder類型的ThreadLocal變量,這樣子每個線程都擁有自己的DocumentBuilder對象,能夠同時轉換多個xml文件。代碼如下:
private static ThreadLocal docBuildeIns = new ThreadLocal() {protected DocumentBuilder initialValue() {try {return DocumentBuilderFactory.newInstance().newDocumentBuilder();} catch (ParserConfigurationException e) {String msg = "DocumentBuilder 對象初始化失敗!";log.error(msg, e);throw new IllegalStateException(msg, e);}} };解析xml文件時的調用方法:
docBuildIns.get().parse(File);get()方法返回此線程局部變量的當前線程副本中的值。如果變量沒有用于當前線程的值,則先將其初始化為調用 initialValue() 方法返回的值。
?
總結
以上是生活随笔為你收集整理的FWK005 parse may not be called while parsing的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 详细讲解在Spring中进行集成测试Ab
- 下一篇: Hibernate sql查询