2011-11-02 72 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