简体   繁体   中英

JAXB Java generating XML, Why lowercase?

When I run this code:

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

I get

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

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?

This is what I actually wanted to have:

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

Thanks!

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

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

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

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:

For specific elements...

@XmlElement( name = "Code")
private String code;

For the object....

@XmlRootElement(name="Employee")
public class Employee{ ...

My solution after put @XmlElement(name="Xxxxx") to fields and used XStream.aliasField(). This is more generic because it uses annotations and scans other class calls in the same package.

import java.lang.reflect.Field;
import java.util.Map;
import java.util.TreeMap;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlAttribute;
import com.thoughtworks.xstream.XStream;
import my.MyClassGeneratedFromXsdToJaxB;

public class TestChangeFirstLetterXml {

public static void main(String[] args) throws ClassNotFoundException {
    MyClassGeneratedFromXsdToJaxB myClassGeneratedFromXsdToJaxB=new MyClassGeneratedFromXsdToJaxB();
    XStream xstream = new XStream();
    xstream.autodetectAnnotations(true);
    xstream = makeAliasAnnotatedFields(xstream, MyClassGeneratedFromXsdToJaxB.class, "FirstTagOrRoot");
    //System.out.println(xstream.toXML(myClassGeneratedFromXsdToJaxB));
}

public static XStream makeAliasAnnotatedFields(XStream xstream, Class myclass, String firstTag)
        throws ClassNotFoundException {
    xstream.alias(firstTag, myclass);
    Map<String, Object[]> aliaslist = getListAlias(myclass);
    for (String key : aliaslist.keySet()) {
        Object[] aliasvalue = new Object[3];
        aliasvalue = aliaslist.get(key);
        String xmlTag = new String((String) aliasvalue[0]);
        Class<?> classJaxb = (Class<?>) aliasvalue[1];
        String tagToRename = new String((String) aliasvalue[2]);
        xstream.aliasField(xmlTag, classJaxb, tagToRename);
        System.out.println("AliasField " + xmlTag + " " + classJaxb.getName() + " " + tagToRename);

    }
    return xstream;
}

public static Map<String, Object[]> getListAlias(Class<?> classToCheck)
        throws ClassNotFoundException {
    /* Read recursive fields of the class */
    Field[] fs = classToCheck.getDeclaredFields();
    String annotationsPackage = classToCheck.getPackage().getName();
    String classSimpleName = new String(classToCheck.getSimpleName());
    /* it is necessary avoid loop */
    Map<String, Object[]> aliasStart = new TreeMap<String, Object[]>();
    /* */
    for (int i = 0; i < fs.length; i++) {
        String nameField = fs[i].getName();
        String classFieldName = new String(fs[i].getType().getName());
        String nameXmlXsd = new String("");
        String idkey = new String(annotationsPackage + ".");
        if (fs[i].isAnnotationPresent(javax.xml.bind.annotation.XmlElement.class)) {
            XmlElement atrib = fs[i].getAnnotation(XmlElement.class);
            nameXmlXsd = new String(atrib.name());
            idkey = new String(idkey + classSimpleName + ".Element." + nameField);
        } else if (fs[i].isAnnotationPresent(javax.xml.bind.annotation.XmlAttribute.class)) {
            XmlAttribute atrib = fs[i].getAnnotation(XmlAttribute.class);
            nameXmlXsd = new String(atrib.name());
            idkey = new String(idkey + classSimpleName + ".Type." + nameField);
        }

        if (aliasStart.containsKey(idkey)) /* avoid loop */
            continue;
        if (nameXmlXsd.equals("Signature")) // My particular condition
            continue;

        if (!nameXmlXsd.equals(classFieldName)) {
            // xstrem.aliasField(a,b,c)
            Object[] alias = new Object[3];
            alias[0] = new String(nameXmlXsd);
            alias[1] = classToCheck;
            alias[2] = new String(nameField);
            aliasStart.put(idkey, alias);
        }
        if (classFieldName.indexOf(annotationsPackage) > -1) {
            Class<?> c = Class.forName(classFieldName);
            Map<String, Object[]> aliaslist = getListAlias(c);
            for (String key : aliaslist.keySet()) {
                Object[] aliasvalue = new Object[3];
                aliasvalue = aliaslist.get(key);
                aliasStart.put(key, aliasvalue);
            }
        }

    }
    return aliasStart;
}
}

An alternative answer, if JAXB is not a MUST, then you can actually use org.json jar to convert the object to a JSONObject, then from there, you can use the XML object to convert the JSONObject to an XML. You will need a few tweaks before it can be a standalone XML.

A code snippet example:

public static String getXMLString(Object o){
    JSONObject json = new JSONObject(o);
    String result = 
        "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>" + 
        XML.toString(json, o.getClass().getSimpleName());
    return result;
}

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