2013-10-07 38 views
1

XML我使用这个代码从Java Bean或使用JAXB

JAXBContext jc = JAXBContext.newInstance(Bookdata.class); 
Bookdata bookdata=new Bookdata(); 
Marshaller marshaller = jc.createMarshaller(); 
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); 
marshaller.marshal(bookdata, (OutputStream) output); 

但它生成XML创建XSD但我需要XSD 我需要这样的创建XSD:

<ArrayOfCommandInfoDTO> 
    <CommandInfoDTO> 
     <a:allowAddingGameCenterFriends>true</a:allowAddingGameCenterFriends> 
     <a:enter code here>allowAppInstallation>true</a:allowAppInstallation> 
     <a:allowAssistant>true</a:allowAssistant> 
     <a:allowAssistantWhileLocked>true</a:allowAssistantWhileLocked> 
     <a:allowCamera>true</a:allowCamera> 
    </CommandInfoDTO> 
</ArrayOfCommandInfoDTO> 

所以请告诉我如何从Java Beans或XML创建XSD

+0

请参阅http://pic.dhe.ibm.com/infocenter/wasinfo/v6r1/index.jsp?topic=%2Fcom.ibm.websphere.wsfep.multiplatform.doc%2Finfo%2Fae%2Fae% 2Ftwbs_jaxbjava2schema.html –

+0

http://stackoverflow.com/questions/5239284/can-any-tool-generate-xsd-from-xml –

+0

@GV,但我需要在Web应用程序中实现此。 – pankaj369

回答

1
public class ObjToSchema { 

    public static void main(String[] args) throws IOException, JAXBException { 
     // TODO Auto-generated method stub 
     (JAXBContext.newInstance(Bookdata.class)).generateSchema(new DataSchemaOutputResolver()); 
    } 
} 


public class DataSchemaOutputResolver extends SchemaOutputResolver { 
    @Override 
    public Result createOutput(String arg0, String arg1) throws IOException { 
     // TODO Auto-generated method stub 
     return new StreamResult(new File("d:/data.xsd")); 
    } 
} 

希望这将解决您的问题。