JAXB unmarshal返回null值 [英] JAXB unmarshal returning null values

查看:680
本文介绍了JAXB unmarshal返回null值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用JAXB将XML文件转换为java对象

I am using JAXB to convert XML file to java object

我在Web上的示例中看了很多但是当我在我的对象中仍然得到空值将它解组为java对象

I have looked a lot in the examples on the web but still get null values in my object when I unmarshall it to a java object

我想念的是什么?

File file = new File("BootloaderProtocol.xml");

        JAXBContext jaxbContext = JAXBContext.newInstance(Command.class);

        Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
        Command commanda = (Command) jaxbUnmarshaller.unmarshal(file);
        System.out.println(commanda);

我的班级:

 @XmlRootElement(name="Command")
 public class Command {


String COMMAND_ID;
String COMMAND_NAME;
String COMMAND_CODES;


public String getCOMMAND_ID() {
    return COMMAND_ID;
}

@XmlElement
public void setCOMMAND_ID(String COMMAND_ID) {
    this.COMMAND_ID = COMMAND_ID;
}
public String getCOMMAND_NAME() {
    return COMMAND_NAME;
}

@XmlElement
public void setCOMMAND_NAME(String COMMAND_NAME) {
    this.COMMAND_NAME = COMMAND_NAME;
}

public String getCOMMAND_CODES() {
    return COMMAND_CODES;
}

@XmlElement
public void setCOMMAND_CODES(String COMMAND_CODES) {
    this.COMMAND_CODES = COMMAND_CODES;
}

}

这是我的XML文件:

 <Command>
    <COMMAND>
        <COMMAND_ID>0xFE01</COMMAND_ID>
        <COMMAND_NAME>Start bootloader</COMMAND_NAME>
        <COMMAND_CODES>EE120301FE0900</COMMAND_CODES>
    </COMMAND>


  </Command>


推荐答案

这是正确的xml结构:

This is the correct xml structure:

<Command>
    <COMMAND_ID>0xFE01</COMMAND_ID>
    <COMMAND_NAME>Start bootloader</COMMAND_NAME>
    <COMMAND_CODES>EE120301FE0900</COMMAND_CODES>
</Command>

使用此xml尝试使用您的代码。在代码中使用xml文件的正确路径。

Try your code with this xml. Use the correct path of the xml file in the code.

顺便说一下,我已经用这个xml测试了你的代码,它运行正常。

Btw, I have tested your code with this xml and it works fine.

这篇关于JAXB unmarshal返回null值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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