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

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

问题描述

例如,我有一个导入另一个模式的简单模式.第二个模式(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 任务从这个模式生成类.我遇到的问题是我正在与之交互的第三方应用程序对于命名空间来说是特殊的.这种情况下我需要一个字符串值,所以我必须对其进行序列化.我为此使用样板代码.

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();
}

这给了我一些类似的东西

Which gives me something along the lines of

<?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" 之类的东西,也就是说,它们是基于 name 赋予命名空间.必须是第三方"才能使他们的软件正常工作.

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

这显示了如何做到这一点.

This shows how to do it.

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

万一链接失效的关键位:

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 在编组为字符串时使用的命名空间前缀?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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