简体   繁体   中英

jaxb xml attribute binding annotations

Hi everybody,

I'm rather new to jaxb annotations. I'm ok with binding a general xml through the use of annotations. I'd like to learn how to to bind a more complexed xml. So far I've read a few other posts; mainly this post but I'm still kinda lost.

The example xml I'm trying to work with is this one:

<request>
    <model> text </model>
    <file name = aFileName> file contents</file>
</request>

where aFileName would depend on whatever the file name in a folder is and file contents would be the actual contents of that file

Also, another thing that has me a little confused is how I would assign the values of the element. I know that marshalling / unmarshalling when using data transfer objects would normall be ObjInst.setter("value"). Then you pass the entire object to the marshaller / unmarshallor. How would you do this with an element that has a particular attribute name? Any help you guys would be able to offer me would be greatly appreciated.

This is the code I have so far: RequestMsg class

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
import org.eclipse.persistence.oxm.annotations.XmlPath;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(propOrder = { "model", "file",})
@XmlRootElement(name = "request")

public class RequestMsg implements Serializable {
    private static final long serialVersionUID = -5003915336631618163L;

    @XmlElement()
    private String model;

    private ElemWithAttr file;

    @XmlPath("file/@myAttr")
    private String myAttr;

// CLASS GETTERS & SETTERS
    public ElemWithAttr getFile(){
        return file;
    }

    public String getModel() {
        return model;
    }

    public void setFile(ElemWithAttr file) {
        this.file = file;
    }

    public void setModel(String model) {
        this.model = model;
    }
}

This is the code I have for the ElemWithAttr Class:

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlValue;


public class ElemWithAttr {

    @XmlValue
    public String content;

    @XmlAttribute
    public String myAttr;


// CLASS GETTERS & SETTERS
    public String getContent() {
        return content;
    }

    public String getMyAttr() {
        return myAttr;
    }

    public void setContent(String audioString) {
        this.content = audioString;
    }

    public void setMyAttr(String myAttr) {
        this.myAttr = myAttr;
    }
}

Try @XmlAnyAttribute . This is the only standard annotation that allows you to change the attribute name depending on some value in the object. Maps to Map<QName, String> - fully qualified attribute name to value.

So I finally got the example I was working on to work. I'm putting my solution in case maybe one day it helps someone else out.

This solution DOESN'T use the @XmlAnyAttribute. Instead I got it to work using @XmlAttribute & @XmlValue. Basically, the variables in the ElemWithAttr class need to be set first. Then, pass the instance of ElemWithAttr to the setFile method in the RequestMsg class.

In other words, something like this:

ElemWithAttr xml = new ElemWithAttr();
xml.setValue("MEMEMEME");
xml.setName("HAHAHAHA");

RequestMsg req = new RequestMsg();
req.setModel("TEST");
req.setFile(xml);

Output:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<request>
    <model>test</model>
    <file name="HAHAHAHA">MEMEMEME</file>
</request>

Here are the modifications to my classes:

RequestMsg Class:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(propOrder = { "model", "file",})
@XmlRootElement(name = "request")

public class RequestMsg implements Serializable {
    private static final long serialVersionUID = -5003915336631618163L;

    @XmlElement()
    protected String model;
    @XmlElement()
    protected ElemWithAttr file;

// CLASS GETTERS & SETTERS
    public ElemWithAttr getFile(){
        return file;
    }

    public String getModel() {
        return model;
    }

    public void setFile(ElemWithAttr file) {
        this.file = file;
    }

    public void setModel(String model) {
        this.model = model;
    }
}

ElemWithAttr Class:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "file")
public class ElemWithAttr {


    @XmlAttribute(name = "name")
    protected String name;

    @XmlValue
    protected String Value;

// CLASS GETTERS & SETTERS
    // GETTERS
    public String getName() {
        return name;
    }

    public String getValue() {
        return Value;
    }

    // SETTERS  
    public void setName(String name) {
        this.name = name;
    }

    public void setValue(String value) {
        Value = value;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM