4

ルート ノードにのみ xmlns 属性を追加したいのですが、名前空間をルート要素に追加すると、後続のすべての子要素も同じ xmlns 属性を取得します。xmlns 属性を単一のノードに追加し、その子ノードには追加しないようにするにはどうすればよいですか? コード:

public String toXml() {

        Document document = DocumentHelper.createDocument();
        Element documentRoot = document.addElement("ResponseMessage");
        documentRoot.addNamespace("",getXmlNamespace())
                .addAttribute("xmlns:xsi", getXmlNamespaceSchemaInstance())
                .addAttribute("xsi:schemaLocation", getXmlSchemaLocation())
                .addAttribute("id", super.getId());

        Element header = documentRoot.addElement("Header");
        buildHeader(header);

        Element body = documentRoot.addElement("Body");
        buildProperties(body);

        body.addElement("StatusMessage").addText(this.getStatusMessage().getMessage());

        return document.asXML();


    }
4

2 に答える 2

6

OK、新しい答え。

要素を特定の名前空間に所属させたい場合は、必ずその名前空間に要素を作成してください。Qname引数の 1 つとして持つメソッドを使用します。名前空間を持たない要素を作成する場合、DOM4J は (不本意ながら) 仕様に対応するために名前空間宣言を追加する必要があります。

あなたの例は少し編集されています。QName は使用しませんでしたが、各要素に名前空間 uri を指定しました。

public static String toXml() {

    Document document = DocumentHelper.createDocument();
    Element documentRoot = document.addElement("ResponseMessage",
            getXmlNamespace());
    documentRoot.addAttribute(QName.get("schemaLocation", "xsi", "xsi-ns"),
            "schema.xsd").addAttribute("id", "4711");

    Element header = documentRoot.addElement("Header");

    Element body = documentRoot.addElement("Body", getXmlNamespace());
    // buildProperties(body);

    body.addElement("StatusMessage", getXmlNamespace()).addText("status");

    return document.asXML();

}

private static String getXmlNamespace() {
    return "xyzzy";
}

public static void main(String[] args) throws Exception {

    System.out.println(toXml());
}

出力として生成されます:

<?xml version="1.0" encoding="UTF-8"?>
<ResponseMessage xmlns="xyzzy" xmlns:xsi="xsi-ns" xsi:schemaLocation="schema.xsd" id="4711">
<Header/><Body><StatusMessage>status</StatusMessage></Body>
</ResponseMessage>

更新 2:

schemaLocationまた、属性の宣言方法を変更したことにも注意してください。名前空間の宣言を手動で管理する必要はまったくありません。これはライブラリによって処理されます。

ただし、名前空間のデラレーションを追加すると便利な場合があります。主に名前空間 X 要素を含むドキュメントがあり、名前空間 Y を持ついくつかの子要素がドキュメント内に広がっている場合、ルートで Y の名前空間バインディングを宣言します。要素で、子要素で繰り返される多くの名前空間宣言を保存できます。

于 2011-10-18T21:36:12.927 に答える
2

これが方法です。ちょっとしたハックですが、あなたが望むことをします:

public static String toXml() {

Document d = DocumentHelper.createDocument();
Namespace rootNs = new Namespace("", DEFAULT_NAMESPACE); // root namespace uri
Namespace xsiNs = new Namespace("xsi", XSI_NAMESPACE); // xsi namespace uri
QName rootQName = QName.get(rootElement, rootNs); // your root element's name

Element root = d.addElement(rootElement);
root.setQName(rootQName);
root.add(xsiNs);
root.addAttribute("xsi:schemaLocation", SCHEMA_LOC)
.addAttribute("id", super.getId());

Element header = documentRoot.addElement("Header");

Element body = documentRoot.addElement("Body", getXmlNamespace());
// buildProperties(body);

body.addElement("StatusMessage", getXmlNamespace()).addText("status");

return document.asXML();

}
于 2011-12-07T07:18:55.567 に答える