2014-01-16 21 views
2

當我創建JDOM文檔(文檔DOC =新的文檔();),默認情況下我只看到版本和編碼在XML頭:創建XML與JDOM,如何設置獨立=「否」屬性

<?xml version="1.0" encoding="utf-8" ?> 

如何添加獨立的屬性來獲得:

<?xml version="1.0" encoding="utf-8" standalone="no" ?> 

回答

3

頁眉通常由XMLParser的剝離文件到達前JDOM。我敢肯定你的意思是你要找的輸出從JDOM,它增加了XML聲明回去。

您可以調整如何將XML聲明是通過創建一個custom XMLOutput processor處理......與此自定義類,覆蓋printDeclaration方法並將其更改爲你所需要的....

public static final XMLOutputProcessor XMLOUTPUT = new AbstractXMLOutputProcessor() { 
    @Override 
    protected void printDeclaration(final Writer out, final FormatStack fstack) throws IOException { 
     write(out, "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"no\"?> "); 
     write(out, fstack.getLineSeparator()); 
    } 
}; 

然後,當你想用這個,你把它傳遞給你的XMLOutputter爲:

XMLOutputter xout = new XMLOutputter(Format.getPrettyFormat(), XMLOUTPUT); 
xout.output(doc, System.out); 

它很明顯這樣做的機制相當麻煩。我會研究有哪些替代方案,並且可能會在未來的版本中解決這個問題。