1

对于给定的类结构(跳过注释等):

class A {
 public B getObjectB {}
}

class B {
 public String getHello { return " world"; }
}

我生成正确的 XSD 和 XML 输出没有问题:

<A>
 <B>
  <hello>world</hello>
 </B>
</A>

问题是,我需要打破它:首先,XSD 应该保持原样 - 完整。但是在编组A时,我需要得到这样的东西:

<A>
 someStringForA
</A>

(所以 B 被呈现为一些计算的字符串)。同时,在编组 B(作为根)时,我仍然需要获得“正常”输出。

我尝试使用 XmlAdapters,但使用 @XmlJavaTypeAdapter 也更改了 XSD。通过 Marshaller.setAdapter(...) 设置适配器显然(http://stackoverflow.com/questions/6110757/jaxb-xml-adapters-work-via-annotations-but-not-via-setadapter/6112149#6112149)不行。

某种解决方案是,如果有可能“关闭”@XmlJavaTypeAdapter(XSD 由 JUnit“手动”生成,允许进行一些切换甚至 hack)

谢谢你的帮助!

4

1 回答 1

1

注意: 我是 E clipseLink JAXB (MOXy)负责人,也是JAXB (JSR-222)专家组的成员。

备用映射 - oxm.xml

如果您使用 MOXy 作为您的 JAXB 提供程序,您可以使用外部映射文件为您的域模型提供备用映射(请参阅:http://blog.bdoughan.com/2010/12/extending-jaxb-representing-annotations。 html ).

<?xml version="1.0"?>
<xml-bindings
    xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
    package-name="forum13843624">
    <java-types>
        <java-type name="A">
            <java-attributes>
                <xml-value java-attribute="objectB"/>
            </java-attributes>
        </java-type>
        <java-type name="B">
            <java-attributes>
                <xml-value java-attribute="hello"/>
            </java-attributes>
        </java-type>
    </java-types>
</xml-bindings>

Java 模型

一个

import javax.xml.bind.annotation.*;

@XmlRootElement(name="A")
class A {

    private B b;

    @XmlElement(name="B")
    public B getObjectB() {
        return b;
    }
    public void setObjectB(B b) {
        this.b = b;
    }

}

import javax.xml.bind.annotation.XmlElement;

class B {

    @XmlElement
    public String getHello() {
        return " world";
    }

}

jaxb.properties

要将 MOXy 指定为您的 JAXB 提供程序,您需要包含一个jaxb.properties与您的域模型在同一包中命名的文件,其中包含以下条目(请参阅: http ://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as -your.html )。

javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory

演示代码

import java.util.*;
import javax.xml.bind.*;
import org.eclipse.persistence.jaxb.JAXBContextProperties;

public class Demo {

    public static void main(String[] args) throws Exception {
        B b = new B();
        A a = new A();
        a.setObjectB(b);

        JAXBContext jc = JAXBContext.newInstance(A.class);
        marshal(jc, a);

        Map<String, Object> properties = new HashMap<String, Object>();
        properties.put(JAXBContextProperties.OXM_METADATA_SOURCE, "forum13843624/oxm.xml");
        JAXBContext jc2 = JAXBContext.newInstance(new Class[] {A.class}, properties);
        marshal(jc2, a);
    }

    private static void marshal(JAXBContext jc, A a) throws Exception {
        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(a, System.out);
    }

}

输出

下面是运行演示代码的输出。注意同一个对象图是如何以两种不同的方式编组的。

<?xml version="1.0" encoding="UTF-8"?>
<A>
   <B>
      <hello> world</hello>
   </B>
</A>
<?xml version="1.0" encoding="UTF-8"?>
<A> world</A>
于 2012-12-12T20:02:29.873 回答