Generating Java classes from XML file - java

How can I get java classes from an xml file?
In this situation, I don't have an XML schema and this way I can't use JAXB, Castor or other xml binding API as far as I know.

You can generate schema from XML file using certain tools. Then, use Apache XMLBEANS to create your classes.

XStream is great for XML -> objects and vice versa. Fast, lightweight, and works well without any schema.

Altova is also the best to generate java Classes from XML/XSD

Related

Other replacements for Serialization

I have XML Configuration file and I need to load/save it so What are best ways to Parse XML Files for Load/Save configurations other than Serialization as I don't like the option of Serialization
For a Java application I serialized a pretty complex application state using XStream. For the most part this works extremely good. If the object is really simple this should not be a problem. Another simple alternative I often use to transfer data from Object <-> XML is JAXB with annotations on the Objects, or if the XML structure is the master with an XML Schema from which the classes can be generated.
For C# you could use IXmlSerializable, or DataContract. Multiple answers can be found on SO using both these classes. See this to see how to use DataContract.

is there a way in java to create xsd schema?

Is there a way in Java to create an XSD schema? I am trying to create a new schema .xsd file according to the number of attributes and their type given to me as input. Is there a package that can help me to do this job?
JAXB Schemagen can generate XSD Schema from Java Classes.
try jlibs library. you can see you to create how to create xml schema using jlibs.xml.xsd.XSDocument at the end of the wiki
Use the eclipse's xsd api has the ability to create, parse and update a xsd. Refer to their documentation for how-to details.
With Apache XmlSchema you can construct XML schemata in-memory and convert them to string.
In my project I've used the JAXB compiler to generate an object model "home made API" taking the XML schema as XJC input. You can find the file here: http://www.w3.org/2001/XMLSchema.xsd
Another solution is to use the Eclipse project MDT XSD (http://www.eclipse.org/modeling/mdt/?project=xsd#xsd) but I found it not well documented and easily integratable in a Maven build.

Programatic generation of xml from a xsd that uses other xsds

I have a xsd that in turn uses/imports a set of xsds.
I would like to programtically generate sample xml from the xsd. The xml must contain all elements and attributes populated with example data based on data type.
How can I do this using eclipse api classes?
Also are there any other tools that accomplish this task and can be evoked in a java program or a batch file?
Any pointers to examples/documentation/api is highly appreciated.
Thanks in advance.
if I am reading your question correctly, I believe what you are trying to do is programmatically generate (i.e. using Java) XML documents based on an XML Schema Document (which may in turn import other supporting XSD's).
You may wish to have a look at Oracle/Sun's JAXB (Java Architecture for Xml Binding) which you can find more info about here:
http://jaxb.java.net/
JAXB works with J2SE-SDK and/or IDEs - such as Netbeans or Eclipse, and permits you to unmarshall (read XML documents into mapped Java Objects) or marshall (write Java objects as XML documents) as required. Standard mappings (known as binding declarations) are provided based on valid XML Schema provided to JAXB. You can also provide binding declarations through custom annotations directly within your XML Schema files or by using external JAXB declarations.
Another alternative (similar to JAXB) is Apache's XML-Beans.
Hope this helps!

Java equivalent of .NET's System.Xml.Schema classes?

The .NET Framework exposes a rather useful set of classes as part of the System.Xml.Schema namespace.
Among other things, classes such as XmlSchema and XmlSchemaElement provide a useful API for defining/writing and parsing/reading/traversing XML schema files.
Does an equivalent library exist for Java?
The tools to accomplish this are there, but I don't think there's anything specific like that in Java. What I would do is run JAXB on the XML Schema schema (http://www.w3.org/2001/XMLSchema), which will generate Java classes corresponding to the XSD constructs. Then you can use those to read/write XML schemas, and validate against the XML Schema schema.

Generate xsd schema from java source

Given a few hundred java source files how can I generate an xsd schema that describes their class hierarchy, attributes and methods?
Most of the resources I've seen describe how to convert java to xml given the schema but how is that schema created? I did find Simple but that requires me to alter the source which I am not allowed to do.
Apache axis provides the java2wsdl tool. Sure, you didn't ask for a wsdl but this tool should generate schemas (as needed) too.
So it's worth trying: implement some dummy interfaces with methods, that just use the types from your 'hundreds' of files, generate a wsdl and delete everything but the schemas.
XSD schemas in themselves are recipies allowing a program to say whether a given XML document "conforms"/"does not conform" to the schema.
For this to make sense for Java classes, you have to define a conversion scheme from Java source to XML, and that XML format can then have an XSD, which may or may not be easily derivable programatically.
What Java -> XML mapping do you use?

Categories