是否可以自定义JAXB在编组到String时使用的名称空间前缀? [英] Is it possible to customize the namespace prefix that JAXB uses when marshalling to a String?

查看:92
本文介绍了是否可以自定义JAXB在编组到String时使用的名称空间前缀?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我有一个导入另一个模式的简单模式。第二个模式(urn:just:attributes,just-attributes.xsd)只定义了一个属性组。

For example, I've got a simple schema which imports another schema. The second schema (urn:just:attributes, just-attributes.xsd) just defines an attribute group.

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" 
    targetNamespace="http://www.example.org/MySchema"
    xmlns:tns="http://www.example.org/MySchema" 
    elementFormDefault="qualified"
    xmlns:ja="urn:just:attributes">

    <import schemaLocation="just-attributes.xsd" namespace="urn:just:attributes"/>

    <element name="MyElement">
        <complexType>
            <attributeGroup ref="ja:AttributeGroup"/>
        </complexType>
    </element>
</schema>

我正在使用Metro xjc Ant任务来生成此模式的类。我遇到的问题是我正在与之交互的第三方应用程序是名称空间特有的。这种情况我需要一个String值,所以我必须序列化它。我使用样板代码。

I'm using the Metro xjc Ant task to generate classes off of this schema. The problem I'm running into is that the third party application I'm interacting with is peculiar about namespaces. This case I need a String value, so I have to serialize it. I use boilerplate code for this.

private static <T> String marshal(T object) throws JAXBException{
    OutputStream outputStream = new ByteArrayOutputStream();
    JAXBContext jaxbContext = JAXBContext.newInstance(object.getClass());
    Marshaller marshaller = jaxbContext.createMarshaller();
    marshaller.marshal(object, outputStream);
    return outputStream.toString();
}

这给了我一些

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns2:MyElement xmlns:ns1="urn:just:attributes" xmlns:ns2="http://www.example.org/MySchema" ns1:attrib1="1234" ns1:attrib2="5678"/>

我遇到的问题是这个第三方需要类似 xmlns:thirdpartyns =urn:just:attributes,也就是说,它们基于命名空间的名称进行解析。 已经成为他们的软件工作的第三方。

The problem I have is that this third party expects something like xmlns:thirdpartyns="urn:just:attributes", which is to say, they are parsing based on the name given to the namespace. It has to be "thirdpartyns" for their software to work.

有没有人知道如何解决这个问题,而不是做一个查找和在结果字符串中替换?可能是自定义绑定规则?

Does anyone know of a way around this, short of doing a find and replace in the resulting string? A custom binding rule perhaps?

推荐答案

http://hwellmann.blogspot.com/2011/03/jaxb-marshalling-with-custom-namespace.html

这显示了如何做。

另一个:
http://www.systemmobile.com/?p=280

Key链接也死掉的位:

Key bits in case that link dies too:

com.sun.xml.bind.marshaller包中的NamespacePrefixMapper类。抽象类有一种方法可以实现:

the NamespacePrefixMapper class, found in the com.sun.xml.bind.marshaller package. The abstract class has one method to implement:

public abstract String getPreferredPrefix(  
     String namespaceUri,         
     String suggestion,         
     boolean requirePrefix); 

然后

Marshaller marshaller =        
    jaxbContext.createMarshaller();        
marshaller.setProperty("com.sun.xml.bind.namespacePrefixMapper",        
    new MyNamespacePrefixMapper());  

如果您还使用javax.xml.xpath.XPath,您的NamespacePrefixMapper也可以实现javax。 xml.namespace.NamespaceContext,将名称空间自定义集中在一个类中。

If you’re also using javax.xml.xpath.XPath, your NamespacePrefixMapper can also implement javax.xml.namespace.NamespaceContext, centralizing your namespace customization in a single class.

这篇关于是否可以自定义JAXB在编组到String时使用的名称空间前缀?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆