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.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' に文字 ":" を含めることはできません。"

修正を手伝ってください。

4

1 に答える 1

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);
// ...
于 2014-07-13T04:01:51.653 に答える