Java 如何通过包含名称空间信息来创建XML元素?

Java 如何通过包含名称空间信息来创建XML元素?,java,xml,jdom,jdom-2,Java,Xml,Jdom,Jdom 2,我试图通过将名称空间信息作为属性来创建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

我试图通过将名称空间信息作为属性来创建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”不能包含字符“:”


请帮助我进行修复。

将名称空间声明添加到根元素,并使用对象,而不是在属性名称中包含名称空间前缀:

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);
// ...