2015-01-26 48 views
1

嗨孩子這是我做的,用於生成XML代碼:公理的OMElement產生空默認名稱空間

OMFactory factory = OMAbstractFactory.getOMFactory(); 
OMNamespace nsSequence = factory.createOMNamespace("http://ws.apache.org/ns/synapse", ""); 
OMElement rootSequence = factory.createOMElement("sequence",nsSequence); 


/*<FILTER>*/ 
OMNamespace nsFilter = factory.createOMNamespace("http://org.apache.synapse/xsd", "ns");   
OMElement filter = factory.createOMElement("filter",nsFilter); 
OMAttribute regex = factory.createOMAttribute("regex", null, "applID"); 
OMAttribute source = factory.createOMAttribute("source", null, "get-property('applicationID')"); 

filter.addAttribute(regex); 
filter.addAttribute(source); 

/*<THEN>*/ 
OMElement then = factory.createOMElement("then",null);   

filter.addChild(then); 
rootSequence.addChild(filter); 

生成的代碼是像這樣的:

<sequence xmlns="http://ws.apache.org/ns/synapse"> 
    <ns:filter xmlns:ns="http://org.apache.synapse/xsd" regex="APPP" source="get-property('applicationID')"> 
     <then xmlns=""></then> 
    </ns:filter> 
</sequence> 

的XMLNS =」 「在THEN元素內部對我來說是個大問題。

我使用axiom-api 1.2.14 ...我在某處讀到這是其他人遇到的問題(錯誤)(可能已解決?)。 有沒有辦法解決這個問題,以獲得一個乾淨的XML代碼?或者更好地解決它?

+0

解決方法是將名稱空間添加爲屬性:OMAttribute ns = factory.createOMAttribute(「xmlns:ns」,null,「http://org.apache.synapse/xsd」); filter.addAttribute(ns); ...這種方式應該工作...但我認爲這是一個應該報告的錯誤。 – Alex 2015-01-26 22:03:31

回答

0

你沒有命名空間創建then元素:

OMElement then = factory.createOMElement("then", null); 

因此公理生成xmlns=""從而使元素沒有命名空間,完全按照你要求的。事實上,如果沒有xmlns="",該元素將具有默認名稱空間http://ws.apache.org/ns/synapse,這將是錯誤的。

如果您認爲名稱空間聲明是錯誤的,那麼這可能意味着該元素實際上應該屬於文檔中使用的其他名稱空間之一。如果是這種情況,那麼你應該修復代碼以在正確的命名空間中請求一個元素。

+0

真的,我需要公理不要重寫在父節點中聲明的默認名稱空間。寫xmlns =「」它的確如此。這種行爲存在於以前的公理API發佈中,如下所示:https://issues.apache.org/jira/browse/AXIOM-28處理setDefaultNamespace方法。因此,如果公理允許創建一個沒有默認名稱空間的元素規範。正如在上面的jira鏈接中所寫的,解決該問題的一種方法是將名稱空間指定爲屬性 – Alex 2015-01-27 07:17:12

+0

正如我所說的,這不是一個錯誤。公理是嚴格按照你的代碼要求它做的。如果結果不符合您的期望,請修復您的代碼。 – 2015-01-27 07:58:11

+0

你能寫一個示例代碼,以獲得此? ...我想弄清楚我應該修復什麼。 – Alex 2015-01-27 09:07:11

相關問題