Using JAXB generated class for an element that requires an integer with a pattern

后端 未结 1 695
日久生厌
日久生厌 2020-11-29 05:47

I have an element in my XML Schema that is defined as follows:


    
        

        
1条回答
  •  Happy的楠姐
    2020-11-29 05:57

    You could do the following:

    NumberFormatter

    You can do this by writing your own formatter:

    package forum7182533;
    
    public class NumberFormatter {
    
        public static String printInt(Integer value) {
            String result = String.valueOf(value);
            for(int x=0, length = 7 - result.length(); x

    XMLSchema (format.xsd)

    Then when you are going to generate your classes from your XML Schema:

    
    
    
        
            
                
                    
                
            
        
    
        
            
                
            
        
    
    
    

    bindings.xml

    You will leverage a JAXB bindings file to reference your formatter:

    
        
            
            
                
                    
                        
                    
                
            
        
    
    

    XJC Call

    The bindings file is referenced in the XJC call as:

    xjc -d out -p forum7182533 -b bindings.xml format.xsd
    

    Adapter1

    This will cause an XmlAdapter to be created that leverages your formatter:

    package forum7182533;
    
    import javax.xml.bind.annotation.adapters.XmlAdapter;
    
    public class Adapter1
        extends XmlAdapter
    {
    
    
        public Integer unmarshal(String value) {
            return (forum7182533.NumberFormatter.parseInt(value));
        }
    
        public String marshal(Integer value) {
            return (forum7182533.NumberFormatter.printInt(value));
        }
    
    }
    

    Root

    The XmlAdapter will be referenced from your domain object using the @XmlJavaTypeAdapter annotation:

    package forum7182533;
    
    import javax.xml.bind.annotation.XmlAccessType;
    import javax.xml.bind.annotation.XmlAccessorType;
    import javax.xml.bind.annotation.XmlElement;
    import javax.xml.bind.annotation.XmlRootElement;
    import javax.xml.bind.annotation.XmlType;
    import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
    
    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlType(name = "", propOrder = {
        "number"
    })
    @XmlRootElement(name = "root")
    public class Root {
    
        @XmlElement(required = true, type = String.class)
        @XmlJavaTypeAdapter(Adapter1 .class)
        protected Integer number;
    
        public Integer getNumber() {
            return number;
        }
    
        public void setNumber(Integer value) {
            this.number = value;
        }
    
    }
    

    Demo

    Now if you run the following demo code:

    package forum7182533;
    
    import javax.xml.bind.JAXBContext;
    import javax.xml.bind.Marshaller;
    
    public class Demo {
    
        public static void main(String[] args) throws Exception {
            JAXBContext jc = JAXBContext.newInstance(Root.class);
    
            Root root = new Root();
            root.setNumber(4);
    
            Marshaller marshaller = jc.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            marshaller.marshal(root, System.out);
        }
    }
    

    Output

    You will get the desired output:

    
    
        0000004
    
    

    0 讨论(0)
提交回复
热议问题