JAXB Java 生成 XML,为什么要小写? [英] JAXB Java generating XML, Why lowercase?

查看:22
本文介绍了JAXB Java 生成 XML,为什么要小写?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我运行此代码时:

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;

public class JavaToXMLDemo {
  public static void main(String[] args) throws Exception {
    JAXBContext context = JAXBContext.newInstance(Employee.class);

    Marshaller m = context.createMarshaller();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

    Employee object = new Employee();
    object.setCode("CA");
    object.setName("Cath");
    object.setSalary(300);

    m.marshal(object, System.out);

  }
}

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
class Employee {
  private String code;

  private String name;

  private int salary;

  public String getCode() {
    return code;
  }

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

  public String getName() {
    return name;
  }

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

  public int getSalary() {
    return salary;
  }

  public void setSalary(int population) {
    this.salary = population;
  }
}

我明白了

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<employee>
    <code>CA</code>
    <name>Cath</name>
    <salary>300</salary>
</employee>

哪个是正确的,所以我的问题是为什么它将员工更改为员工?是否可以用大写 E 代替员工打印?

Which is correct, so my question is why does it change the Employee to employee? Is it possible to make it print with uppercase E, instead of employee?

这就是我真正想要的:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Employee>
    <code>CA</code>
    <name>Cath</name>
    <salary>300</salary>
</Employee>

谢谢!

推荐答案

您所看到的行为是标准JAXB (JSR-222) XML 名称到 Java 名称的转换算法.

The behaviour you are seeing is the result of the standard JAXB (JSR-222) XML name to Java name conversion algorithm.

您可以使用 @XmlRootElement 批注来指定名称:

You can use the @XmlRootElement annotation to specify a name:

@XmlRootElement(name="Employee")
@XmlAccessorType(XmlAccessType.FIELD)
class Employee {
    ...
}

我是 EclipseLink JAXB (MOXy)领导,我们有一个扩展,允许您覆盖您可能感兴趣的默认名称转换算法:

I'm the EclipseLink JAXB (MOXy) lead, and we have an extension that allows you to override the default name conversion algorithm that you may be interested in:

这篇关于JAXB Java 生成 XML,为什么要小写?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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