2011-10-18 16 views
3

xmlns属性をルートノードに追加するだけですが、ルート要素に名前空間を追加すると、後続のすべての子要素も同じxmlns属性を取得します。 xmlns属性を1つのノードに追加するにはどうすればよいのですか? コード:Dom4j 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(); 


    } 
+0

意味が明確ではありません。 XMLにシリアライズされると、すべての子要素に対してこの名前空間宣言が表示されますか?どのように名前空間属性を追加しますか?表示するコードと結果がありますか? –

+0

はいxmlにシリアル化されると、ルート要素のすべての子要素に空(xmlns = "")が表示されます。 – Phoenix

答えて

5

OK、新規回答。

要素を特定の名前空間に属したい場合は、その名前空間に要素を作成してください。その引数の1つとしてQnameを持つメソッドを使用します。名前空間を持たない要素を作成する場合、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> 

UPDATE 2:

も注意してください、私はどのような方法を変更したことをschemaLocation属性が宣言されています。実際にネームスペース宣言を手動で管理する必要はありません。これはライブラリによって処理されます。

ただし、名前空間の分離を追加すると便利な場合があります。名前空間Xの要素が主にある文書と名前空間Yの子要素が文書内に広がっていて、 Yをルート要素で使用すると、子要素に多くの名前空間宣言が保存される可能性があります。

+0

私はいくつかのコードで編集しました。 – Phoenix

2

Heres how。その少しのハックですが、あなたが望むことをします:

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

} 
関連する問題