2012-12-20 37 views
-1

可能重複:
Cure for ‘The string 「--」 is not permitted within comments.’ exception?字符串 「 - 」 是不允許的,Java項目

我有一個Java項目來分析和編輯XML文件。 當我嘗試運行項目,我得到以下日誌消息:

 
The string "--" is not permitted within comments. 
Exception in thread "main" org.xml.sax.SAXParseException: The string "--" is not permitted within comments. 
    at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:246) 
... 

我怎樣才能解決這個問題?

+0

向我們展示一些代碼plz – Minion91

+0

和類似的http://stackoverflow.com/questions/1324821/nested-comments-in-xml –

回答

4

不知道您的代碼:

有錯誤是因爲你被告知。在您的XML表格中的某處,您有一條評論,其中包含一個--。比如這樣的事情:

<root> 
    <!-- my comment with -- in it --> 
</root> 

這不是一個結構良好的XML。在評論--是不允許的。解析器將遇到問題以檢測評論的結尾。

+2

可以解釋爲什麼解析器很難嗎?我的意思是「 - >」定義了評論的結尾。那麼,爲什麼解析器不只是忽略「 - 」並且看得更遠呢? – Timo

+0

DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); \t \t \t docFactory.setIgnoringComments(true); \t \t \t docFactory.setValidating(false); \t \t \t \t \t \t DocumentBuilder docBuilder = docFactory.newDocumentBuilder(); \t \t \t doc = docBuilder.parse(file); – user1918246

+1

@Timo +1評論,我剛剛搜索,發現他們保持這樣,以保持與SGML兼容 –

相關問題