开发者

Automatic class model creation based on xml in java

开发者 https://www.devze.com 2023-03-07 01:03 出处:网络
I have an xml file format and using that xml I want to create skeleton classes nee开发者_运维问答ded to serialize and de-serialize that xml. I am using Java and XStream for this.

I have an xml file format and using that xml I want to create skeleton classes nee开发者_运维问答ded to serialize and de-serialize that xml. I am using Java and XStream for this.

There is tool in .net world which creates classes using xml. Is there anything similar in Java world?


I have not used XStream myself, so this solution may not 100% work for you. However, the simplest approach in Java is to use Java's Architectural Binding for XML (JAXB) API and tools. JAXB was included as part of the JDK with the release of Java 6. To generate Java code from an XML schema you would use the xjc command that comes with the JDK. Here is an example:

> xjc schemas\my-schema.xsd -d src\java -p com.company.model

This code generation method will create Java Objects that include JAXB-specific annotations that are used by the Marshaller to map the Java object to its XML format and vice versa. It will also contain number of warnings stating that the code was auto-generated and should not be modified. As long as you are not trying to automatically keep the code in synch with your XML you could ignore these messages.

Now, as I mentioned, this technique does generate JAXB annotated classes, however, the generated code may still be compatible with XStream as I believe XStream uses simple attribute name -> xml node name conversion logic.

0

精彩评论

暂无评论...
验证码 换一张
取 消