1

在下面的场景中,我会喜欢java-type name="SubClass"应用的绑定来设置 SuperClass 上的文本字段。然而事实并非如此。覆盖 bindingsA.xml 有问题吗?根据覆盖规则文档

如果多个文件中出现相同的 java-type,则在后面的文件中设置的任何值都将覆盖前面文件中的值

我需要做什么才能使其工作?

输入:

<?xml version="1.0" encoding="UTF-8"?>
<a text="A text">B text</a>

绑定 A:

<?xml version="1.0"?>
<xml-bindings
 xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
 package-name="test">

 <java-types>        
   <java-type name="SuperClass">
    <xml-root-element name="a"/>
    <java-attributes>
        <xml-element java-attribute="text" xml-path="@text" />
    </java-attributes>
  </java-type>
 </java-types>

</xml-bindings>

绑定 B:

<?xml version="1.0"?>
<xml-bindings
 xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
 package-name="test">

<java-types>        
  <java-type name="SuperClass" xml-transient="true"></java-type>
  <java-type name="SubClass">
    <xml-root-element name="a"/>
    <java-attributes>
        <xml-element java-attribute="text" xml-path="text()" />
    </java-attributes>
  </java-type>
</java-types>

</xml-bindings>

课程:

public class SuperClass {

 private String text;

 public String getText() {
    return text;
 }

 public void setText(String text) {
    this.text = text;
 }
}

public class SubClass extends SuperClass { }

演示:

Map<String, Object> jaxbContextProperties = new HashMap<String, Object>(1);
List<String> bindings = new LinkedList<String>();
bindings.add("bindingsA.xml");
bindings.add("bindingsB.xml");
jaxbContextProperties.put(JAXBContextProperties.OXM_METADATA_SOURCE, bindings);
JAXBContext jaxbContext = JAXBContextFactory.createContext(new Class[] {SuperClass.class}, jaxbContextProperties);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
SuperClass superClass = (SuperClass)unmarshaller.unmarshal(new File("input.xml"));
System.out.println(superClass.getText()); 

输出:

[EL Warning]: 2013-07-31 16:08:07.771--Ignoring attribute [text] on class [SubClass] as no Property was generated for it.
A text
4

1 回答 1

1

text在超类和子类上以不同方式映射属性有点奇怪。如果这是您真正想做的事情,那么下面是您可以完成此操作的一种方法。

Java 模型

超类

package forum17982654;

public class SuperClass {

    private String text;

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

}

子类

我们将覆盖超类中的访问器方法。这将帮助我们诱使 MOXy 认为它SubClass有自己的属性text

package forum17982654;

public class SubClass extends SuperClass {

    @Override
    public String getText() {
        return super.getText();
    }

    @Override
    public void setText(String text) {
        super.setText(text);
    } 

}

元数据

绑定.xml

在映射文档中,我们将告诉 MOXy 真正的超类SubClassjava.lang.Object

<?xml version="1.0"?>
<xml-bindings
    xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
    package-name="forum17982654">
    <java-types>
        <java-type name="SuperClass">
            <xml-root-element/>
            <java-attributes>
                <xml-attribute java-attribute="text"/>
            </java-attributes>
        </java-type>
        <java-type name="SubClass" super-type="java.lang.Object">
            <xml-root-element/>
            <java-attributes>
                <xml-value java-attribute="text"/>
            </java-attributes>
        </java-type>
    </java-types>
</xml-bindings>

演示代码

下面是一些演示代码,您可以运行它来证明一切正常:

演示

package forum17982654;

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

public class Demo {

    public static void main(String[] args) throws Exception {
        Map<String, Object> jaxbContextProperties = new HashMap<String, Object>(1);
        jaxbContextProperties.put(JAXBContextProperties.OXM_METADATA_SOURCE, "bindings.xml");
        JAXBContext jaxbContext = JAXBContextFactory.createContext(new Class[] {SuperClass.class}, jaxbContextProperties);
        Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();

        StringReader superClassXML = new StringReader("<superClass text='Hello Super Class'/>");
        SuperClass superClass = (SuperClass) unmarshaller.unmarshal(superClassXML);
        System.out.println(superClass.getText());

        StringReader subClassXML = new StringReader("<subClass>Hello Sub Class</subClass>");
        SubClass subClass = (SubClass) unmarshaller.unmarshal(subClassXML);
        System.out.println(subClass.getText());
    }

}

输出

Hello Super Class
Hello Sub Class
于 2013-08-01T15:48:53.350 回答