2011-11-02 73 views
1

匹配的命名空間中JAXB匹配的命名空間/在JAXB解組編組上/解組

我使用JAXB馬歇爾/和解組XML。如果我馬歇爾的XML文件是這樣的:

<om:RequestCreateEvent xmlns:om="http://ossj.org/xml/OrderManagement/v1-0" xmlns:v1="http://ossj.org/xml/Common/v1-5" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:v11="http://ossj.org/xml/Common-CBECore/v1-5" xmlns:v12="http://ossj.org/xml/Common-CBEBi/v1-5"> 
    <om:event> 
     <v1:applicationDN>System/JSR264/ApplicationType/OrderManagement/Application/1-0;1-0-2;ReferenceImplementation/</v1:applicationDN> 
     <v1:eventTime>2008-11-12T07:39:34.896+01:00</v1:eventTime> 
     <om:requestValue xmlns:v1="http://ossj.org/xml/om/ri/omimpl/v1-0" xsi:type="v1:ProductOrderImplValue"> 
      <v13:key xmlns:v1="http://ossj.org/xml/OrderManagement/v1-0" xmlns:v13="http://ossj.org/xml/Common/v1-5" xsi:type="v1:ProductOrderKey"> 
       <v13:type>http://ossj.org/xml/om/ri/omimpl/v1-0#ProductOrderImplValue</v13:type> 
       <v13:primaryKey>12</v13:primaryKey> 
      </v13:key> 
      <v1:requestState>open.not_running.not_started</v1:requestState> 
      <v12:description xsi:nil="true"/> 
     </om:requestValue> 
    </om:event> 
</om:RequestCreateEvent> 

,隨後試圖來解讀它,我得到這個:

<ns4:RequestCreateEvent xmlns="http://ossj.org/xml/Common/v1-5" xmlns:ns2="http://ossj.org/xml/Common-CBECore/v1-5" xmlns:ns3="http://ossj.org/xml/Common-CBEBi/v1-5" xmlns:ns4="http://ossj.org/xml/OrderManagement/v1-0" xmlns:ns5="http://ossj.org/xml/om/ri/omimpl/v1-0" xmlns:ns6="http://ossj.org/xml/Common-CBEDatatypes/v1-5" xmlns:ns7="http://ossj.org/xml/Common-CBELocation/v1-5" xmlns:ns8="http://ossj.org/xml/Common-CBEResource/v1-5" xmlns:ns9="http://ossj.org/xml/Common-CBEService/v1-5" xmlns:ns10="http://ossj.org/xml/Common-CBEProduct/v1-5" xmlns:ns11="http://ossj.org/xml/Common-CBEProductOffering/v1-5" xmlns:ns12="http://ossj.org/xml/Common-CBEParty/v1-5"> 
    <ns4:event> 
     <applicationDN>System/JSR264/ApplicationType/OrderManagement/Application/1-0;1-0-2;ReferenceImplementation/</applicationDN> 
     <eventTime> 
2008-11-12T07:39:34.896+01:00</eventTime> 
     <ns4:requestValue xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ns5:ProductOrderImplValue"> 
      <key xsi:type="ns4:ProductOrderKey"> 
       <type> 
http://ossj.org/xml/om/ri/omimpl/v1-0#ProductOrderImplValue</type> 
       <primaryKey/> 
      </key> 
      <ns5:requestState>open.not_running.not_started</ns5:requestState> 
      <ns3:description xsi:nil="true"/> 
     </ns4:requestValue> 
    </ns4:event> 
</ns4:RequestCreateEvent> 

我需要的時候,以確保在命名空間中使用前綴我馬歇爾jaxb生成的pojo創建的XML文件匹配那些使用 我解組同一個文件。

當我進行編組時,可能有一個使用NamespaceContext的解決方案。但是我不能在NamespaceContext的實現 中對前綴和他們的uris進行硬編碼,因爲我沒有這些信息可用(我使用了大量的模式等)。因此,如果我嘗試使用NamespaceContext,我需要能夠從JAXB unmarshaller獲取前綴和他們的uris,我似乎無法得到它。

因此,如果任何人有任何關於NamespaceContext解決方案或其他方法的建議,我將不勝感激。

+0

你也注意到,' 12'改成''解組時?好奇,如果你能克服這個問題,我遇到這個問題 – JGlass

回答

0

快速查看看起來像名稱空間限定的XML文檔,但使用不同的前綴。不同的JAXB實現控制使用的前綴提供不同的機制,但將涉及您的部分互動:

JAXB RI /地鐵JAXB

的JAXB RI /地鐵都提供了擴展名爲NamespacePrefixMapper

的EclipseLink JAXB(MOXY)

MOXy(我是技術負責人)將使用@XmlSchema註釋中指定的前綴。在下面的示例中,​​前綴將用於http://www.w3.org/2005/Atom名稱空間URI。

javax.xml.bind.annotation.XmlSchema( 
    xmlns = { 
      @javax.xml.bind.annotation.XmlNs(prefix = "atom", namespaceURI = "http://www.w3.org/2005/Atom") 
      }) 
    package org.example.domain; 
+0

謝謝你回到我身邊。通過添加一個擴展NamespaceMapper的類,我可以按照我的意圖獲取XML。不過,我仍然需要在該類的getPreferredPrefix()方法中對前綴及其關聯的命名空間進行硬編碼。在運行時,我沒有這個信息。我可以看到我使用的unmarshaller的UnmarshallingContext有他們,但我似乎無法訪問它們。你知道任何從unmarshaller訪問命名空間/前綴的方法嗎? – belltower