如何使用JAXB将Java对象转换为XML元素属性 [英] How to convert Java objects to XML element attributes using JAXB

查看:570
本文介绍了如何使用JAXB将Java对象转换为XML元素属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用JAXB将Java对象转换为xml以获取以下xml:

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>

关于如何获取XML的答案很多.我经历了所有这些.但是我的问题是如何获得我所显示的XML.它包含一个甚至包含属性的自动关闭标签.

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.

我正在使用Eclipse IDE.请提出一种方法.

I am using Eclipse IDE. Please suggest a method.

这是我的案例类:

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;
}
}

解决方案:

我通过为LazerBanana在第一个答案中建议的Meta创建单独的类来解决了这个问题.

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

推荐答案

这是您的Meta类的外观.

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;
    }
}

这是您的Case类,它是根元素

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();
        }

    }

}

输出:

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

下一次,请尝试做更多研究,我不是专家,我只是在Google上进行了搜索.

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/

这篇关于如何使用JAXB将Java对象转换为XML元素属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆