JAXB Java generating XML, Why lowercase?

前端 未结 4 488
天命终不由人
天命终不由人 2020-12-11 00:49

When I run this code:

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.b         


        
相关标签:
4条回答
  • 2020-12-11 01:29

    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;
    }
    }
    
    0 讨论(0)
  • 2020-12-11 01:30

    For specific elements...

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

    For the object....

    @XmlRootElement(name="Employee")
    public class Employee{ ...
    
    0 讨论(0)
  • 2020-12-11 01:39

    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;
    }
    
    0 讨论(0)
  • 2020-12-11 01:54

    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:

    • http://blog.bdoughan.com/2011/05/overriding-jaxbs-name-mangling.html
    0 讨论(0)
提交回复
热议问题