Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JAXB属性作为元素值_Java_Xml_Jaxb - Fatal编程技术网

Java JAXB属性作为元素值

Java JAXB属性作为元素值,java,xml,jaxb,Java,Xml,Jaxb,我有一个定义为 <ROOT> <CHILD1 VALUE=""/> <CHILD2 VALUE=""/> </ROOT> JAXB中有一些剪切绑定特性: 但我想,对于一些不那么重要的事情来说,这将是相当复杂的 如果没有生成Java POJO,您可以简单地添加方法来直接访问子字段,例如Root.getChild1String(),它将调用Root.getChild1().getValue() 或者您可以更改xml模式。我最后编写了一

我有一个定义为

<ROOT>
    <CHILD1 VALUE=""/>
    <CHILD2 VALUE=""/>
</ROOT>

JAXB中有一些剪切绑定特性: 但我想,对于一些不那么重要的事情来说,这将是相当复杂的

如果没有生成Java POJO,您可以简单地添加方法来直接访问子字段,例如Root.getChild1String(),它将调用Root.getChild1().getValue()


或者您可以更改xml模式。

我最后编写了一个适配器来转换属性以进行反序列化

@XmlElement(name = "CHILD1")
@XmlJavaTypeAdapter(ValueAdapter.class)
private String child1;

public class ValueAdapter extends XmlAdapter<Object, String> {
    private static String VALUE = "VALUE";
    @Override
    public String unmarshal(Object e) throws Exception {
        if (e instanceof ElementNSImpl && ((ElementNSImpl)e).hasAttribute(VALUE)) {
            return ((ElementNSImpl)e).getAttribute(VALUE);
        }
        return null;
    }

    @Override
    public Object marshal(String s) throws Exception {
        return null;
    }
}
@xmlement(name=“CHILD1”)
@XmlJavaTypeAdapter(ValueAdapter.class)
私有字符串child1;
公共类ValueAdapter扩展了XmlAdapter{
私有静态字符串VALUE=“VALUE”;
@凌驾
公共字符串解组器(对象e)引发异常{
if(ElementNSImpl&((ElementNSImpl)e.hasAttribute(VALUE))的实例{
return((ElementNSImpl)e).getAttribute(VALUE);
}
返回null;
}
@凌驾
公共对象封送处理(字符串s)引发异常{
返回null;
}
}
@XmlElement(name = "CHILD1")
@XmlJavaTypeAdapter(ValueAdapter.class)
private String child1;

public class ValueAdapter extends XmlAdapter<Object, String> {
    private static String VALUE = "VALUE";
    @Override
    public String unmarshal(Object e) throws Exception {
        if (e instanceof ElementNSImpl && ((ElementNSImpl)e).hasAttribute(VALUE)) {
            return ((ElementNSImpl)e).getAttribute(VALUE);
        }
        return null;
    }

    @Override
    public Object marshal(String s) throws Exception {
        return null;
    }
}