2

I am trying to create an XML element by including the name space information as its attribute. My code is as follows,

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");

for some reason I am getting the following error,

"Exception in thread "AWT-EventQueue-0" org.jdom2.IllegalNameException: The name "xmlns:xsi" is not legal for JDOM/XML attributes: XML name 'xmlns:xsi' cannot contain the character ":"."

Please help me with the fixes.

fabian
  • 80,457
  • 12
  • 86
  • 114

1 Answers1

3

Add namespace declarations to the root element and use the Namespace object instead of including the namespace prefix in the attribute name:

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);
// ...
fabian
  • 80,457
  • 12
  • 86
  • 114