Java and XML story 1: generate a Java class through XSD

Source: Internet
Author: User

Java and XML story 1: generate a Java class through XSD

JAXB makes the tedious mechanical coding between Java and XML a thing of the past, now you can simply generate the corresponding Java file through the XSD file, I generally use the maven-jaxb2-plugin to generate xsd to the java file.
POM Configuration
Specify the xsd file path, specify the binding file path, and specify the java file path to be generated.


    
   
    org.jvnet.jaxb2.maven2
     maven-jaxb2-plugin  
   
    0.9.0
     
       
          
     
      schema1-generate
           
             
      
       generate
            
           
            
      
       src/main/resources/dir1
              
       
       
        1.xsd
        
              
      
       true
              
      
       ${project.build.directory}/generated-sources/xjc1
              
      
       ${project.build.directory}/generated-sources/xjc1/META-INF/1.episode
                
      
       src/main/resources/dir1
              
       
       
        1.xjb
        
            
         
      
   
  

Binding. xjb Configuration
Implement type Matching and package matching. If the type matching is complex, you may need to implement your own conversion class.


      
   
            
            
                
                 
          
      
   
  

Note1:The new version of plugin can not only generate java class, but also help generate the most important methods of the object: toString, equals, hashCode, clone, as long as you configure the plugin in configuration.


      
   
    true
               -XtoString        -Xequals        -XhashCode        -Xcopyable        
           
                
     
      org.jvnet.jaxb2_commons
                 jaxb2-basics            
     
      
             
        
   
  

Note2:I have encountered two xsd files in the same project with the same node type and different content. Although different packages are specified for generate source, an error is returned, the final solution is to use two executions to parse different xsd files, but I didn't encounter this problem when using the cxf plug-in. I can generate all the desired Java classes in one execution.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.