3

我希望超类的字段在下面的场景中被解组,但事实并非如此。该怎么做才能让它发挥作用?

输入:

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

捆绑:

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

<java-types>        
  <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);
jaxbContextProperties.put(JAXBContextProperties.OXM_METADATA_SOURCE, "bindings.xml");
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 15:09:16.602--Ignoring attribute [text] on class [SubClass] as no Property was generated for it.
null
4

2 回答 2

2

需要在它所属的类上进行映射。

选项 #1 -SuperClass打开地图属性SubClass

如果要将超类属性映射为子类的一部分,则需要在java-type元素上标记父类瞬态。

绑定.xml

<?xml version="1.0"?>
<xml-bindings
    xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
    package-name="forum17981925">
    <java-types>
        <java-type name="SuperClass" xml-transient="true"/>
        <java-type name="SubClass">
            <xml-root-element/>
            <java-attributes>
                <xml-element java-attribute="text" xml-path="text()" />
            </java-attributes>
        </java-type>
    </java-types>
</xml-bindings>

选项 #2 -SuperClass打开地图属性SuperClass

或者,您可以将text属性映射到 上SuperClass,然后由 继承此映射SubClass

绑定.xml

<?xml version="1.0"?>
<xml-bindings
    xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
    package-name="forum17981925">
    <java-types>
        <java-type name="SuperClass">
            <java-attributes>
                <xml-element java-attribute="text" xml-path="text()" />
            </java-attributes>
        </java-type>
        <java-type name="SubClass">
            <xml-root-element/>
        </java-type>
    </java-types>
</xml-bindings>

演示代码

可以运行以下演示代码来证明这两个选项都有效:

输入.xml

<subClass>Hello World</subClass>

演示

import java.io.File;
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();
        SuperClass superClass = (SuperClass)unmarshaller.unmarshal(new File("input.xml"));
        System.out.println(superClass.getText());
    }

}

输出

Hello World
于 2013-07-31T22:50:06.203 回答
0

SuperClass 的字段也可以为null - 因为您的SuperClass的字段没有Setter,因为 Unmarshaller 使用不带参数的 Default 构造函数和Setter来初始化他的字段。或者SuperClass没有@XmlRootElement和@XmlElement注解

于 2015-09-08T08:56:13.180 回答