首页 文章

在JAXB中编组/解组时匹配名称空间

提问于
浏览
1

在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解决方案或其他方法有任何建议,我将不胜感激 .

1 回答

  • 0

    快速查看看起来像命名空间限定的XML文档之后,使用不同的前缀 . 不同的JAXB实现提供了用于控制所使用的前缀的不同机制,但是将涉及您的交互:

    JAXB RI/Metro JAXB

    JAXB RI / Metro提供了一个名为 NamespacePrefixMapper 的扩展:

    EclipseLink JAXB (MOXy)

    MOXy(我是技术主管)将使用 @XmlSchema 注释中指定的前缀 . 在下面的示例中, atom 前缀将用于 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;
    

相关问题