2012-03-13 34 views
7

Tôi muốn tạo XSD cho lớp sautiện ích để tạo xsd từ lớp java

public class Node{ 
    private String value; 
    private List<Node> childrens; 

} 

là gì tiện ích tốt nhất để tạo ra giản đồ XSD cho mã như

Nói chung tôi muốn thực hiện cây đơn giản . Tôi đã sử dụng jaxb để tạo ra các lớp từ lược đồ.

Trả lời

5

Bạn có thể sử dụng generateSchema API trên JAXBContext để tạo ra một lược đồ XML:

import java.io.IOException; 
import javax.xml.bind.*; 
import javax.xml.transform.Result; 
import javax.xml.transform.stream.StreamResult; 

public class Demo { 

    public static void main(String[] args) throws Exception { 
     JAXBContext jc = JAXBContext.newInstance(Node.class); 
     jc.generateSchema(new SchemaOutputResolver() { 

      @Override 
      public Result createOutput(String namespaceURI, String suggestedFileName) 
       throws IOException { 
       return new StreamResult(suggestedFileName); 
      } 

     }); 

    } 

}