2014-07-13 50 views
2

我想通過包含名稱空間信息作爲它的屬性來創建一個XML元素。我的代碼如下,如何通過包含名稱空間信息來創建XML元素?

Element root = new Element("APC_DDF"); 
root.setAttribute("xmlns:xsi","http://www.w3.org/2001/XMLSchema-instance"); 
root.setAttribute("xsi:noNamespaceSchemaLocation","http://localhost/ddf_schema/apc_ddf_1_6.xsd"); 
root.setAttribute("ddfid", this.dataHolder.getDDFId()); 
root.setAttribute("ddfname", this.dataHolder.getDDFName()); 
root.setAttribute("ddfversion", "1"); 
root.setAttribute("canremove", "yes"); 

由於某種原因,我收到以下錯誤,

「在線程異常‘的AWT - EventQueue的 - 0’org.jdom2.IllegalNameException:名稱」的xmlns :xsi「對於JDOM/XML屬性不合法:XML名稱'xmlns:xsi'不能包含字符」:「。」

請幫我修復。

回答

3

添加命名空間聲明到根元素,並使用,而不是包括屬性名的命名空間前綴的Namespace對象:

Namespace xsi = Namespace.getNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance"); 
root.addNamespaceDeclaration(xsi); 
root.setAttribute("noNamespaceSchemaLocation","http://localhost/ddf_schema/apc_ddf_1_6.xsd", xsi); 
// ... 
相關問題