Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
228 views
in Technique[技术] by (71.8m points)

eclipse - How to convert Java objects to XML element attributes using JAXB

How to convert java object to xml using JAXB to get the following xml:

<Case>
  <Version>1.0</Version>
  <Code>457123</Code>
  <Meta uc="Sample" pip="116.0.1.1" lot="P"/>
</Case>

There are many answers regarding how to get XML. I have gone through all those. But my question is how to get the XML as what I have shown. It contains a self-closing tag which even contains attributes.

I am using Eclipse IDE. Please suggest a method.

This is my case class:

import auth.Res.Meta;

@XmlRootElement (name="Case")

public class Test {

private Meta mt;
private String version;
private String code;

@XmlRootElement
public class Meta {

    @XmlAttribute
    private String uc;

    @XmlAttribute
    private String pip;

    public String getUc() {
        return uc;
    }

    public void setUc(String uc) {
        this.uc = uc;
    }

    public String getPip() {
        return pip;
    }

    public void setPip(String pip) {
        this.pip = pip;
    }
}

public Meta getMt() {
    return mt;
}

public void setMt(Meta mt) {
    this.mt = mt;
}

public String getVersion() {
    return version;
}

public void setVersion(String version) {
    this.version = version;
}

public String getCode() {
    return code;
}

public void setCode(String code) {
    this.code = code;
}
}

Solution:

I solved it by creating seperate class for Meta as suggested by LazerBanana in the first answer.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This is how your Meta class should look like.

public class Meta {

    private String uc;
    private String pip;
    private String lot;

    public String getUc() {
        return uc;
    }

    @XmlAttribute
    public void setUc(String uc) {
        this.uc = uc;
    }

    public String getPip() {
        return pip;
    }

    @XmlAttribute
    public void setPip(String pip) {
        this.pip = pip;
    }

    public String getLot() {
        return lot;
    }

    @XmlAttribute
    public void setLot(String lot) {
        this.lot = lot;
    }
}

this is your Case class which is the root element

@XmlRootElement
public class Case {

    private int version;
    private String code;
    private String id;
    private Meta meta;

    public int getVersion() {
        return version;
    }

    @XmlElement
    public void setVersion(int version) {
        this.version = version;
    }

    public String getCode() {
        return code;
    }

    @XmlElement
    public void setCode(String code) {
        this.code = code;
    }

    public String getId() {
        return id;
    }

    @XmlElement
    public void setId(String id) {
        this.id = id;
    }

    public Meta getMeta() {
        return meta;
    }

    @XmlElement
    public void setMeta(Meta meta) {
        this.meta = meta;
    }
}

And this is the marshaling bit to the console and to the file it you want.

public class Main {

    public static void main(String... args) {

        Case fcase = new Case();
        Meta meta = new Meta();

        meta.setLot("asd");
        meta.setPip("sdafa");
        meta.setUc("asgd4");

        fcase.setMeta(meta);
        fcase.setVersion(1);
        fcase.setId("sah34");
        fcase.setCode("code34");

        try {

//            File file = new File("C:\file.xml");
            JAXBContext jaxbContext = JAXBContext.newInstance(Case.class, Meta.class);
            Marshaller jaxbMarshaller = jaxbContext.createMarshaller();

            // output pretty printed
            jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

//            jaxbMarshaller.marshal(fcase, file);
            jaxbMarshaller.marshal(fcase, System.out);

        } catch (JAXBException e) {
            e.printStackTrace();
        }

    }

}

Output:

<case>
  <code>code34</code>
  <id>sah34</id>
  <meta lot="asd" pip="sdafa" uc="asgd4"/>
  <version>1</version>
</case>

Next time please try to do more research i am not an expert and I just googled it.

https://www.mkyong.com/java/jaxb-hello-world-example/


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...