is there a way in java to create xsd schema? - java

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.

Related

xml serialization generator for java without using reflection

Is there an XML serialization framework for Java that does not use reflection, but instead generates static serialization code (Java source) from XSD ?
I've never seen anything that does exactly what you are asking for: generating serialization code from XSD. However, if you're not stuck with an existing XSD schema, Modello may satisfy your requirements.
Modello is used by Maven for parsing pom.xml and settings.xml files. It reads a .mdo file (like this description of the Maven project model), and can generate a Java object model; an XML Schema (XSD) file; and serialisation/de-serialisation code. The serialisation/deserialisation code can use one of a number of XML parser APIs (e.g. JDOM, StAX, etc.). The XML parser API used by Maven itself is xpp3.
Modello can also generate code to convert one version of the model to another. It can generate HTML documentation about your XML format.
If you have an existing XSD, it might be too much work to use modello. But, if you're creating your own XML format, it could be worth starting with modello and generating the XSD.

How to generate JAXB classes from just XML

I need to generate classes from xml that doesn't provide a schema. I understand this is near useless, but the fact is we have xml, it's structured, and we should be able to create a model from the xml. In the past I've done it by hand, but the current xml documents I am working with are quite large and my time would probably be better spent building something that does what I need. But, I am guessing it has already been done, and I just can't find it.
Any pointers?
There are many tools available (a quick google search should fetch you some) that can generate XSD from XML assuming string type for almost everything. You should be able to use that XSD to run JAXB to get classes.
Here's an online tool that lets you do that.
And here is a screen cap:
From your xml file, you can create a XML Schema Definition (XSD) file. Once you have the XSD, you'll be able to generate the code, need it be for java, C#, C++, or all of the above.
If you have Visual Studio, you can use xsd.exe to generate the XSD file.
References:
www.xmlforasp.net/CodeBank/System_Xml_Schema/BuildSchema/BuildXMLSchema.aspx
msdn.microsoft.com/en-us/library/x6c1kb0s(v=vs.71).aspx
Command Syntax: xsd file.xml [/outputdir:directory]
so "C:\Program Files\Microsoft SDKs\Windows\v6.0A\Bin\x64\xsd.exe"
example.xml yields a example.xsd file in the same directory as the
xml file.
XSD to Java:
Reference:
http://www.javaworld.com/javaworld/jw-06-2006/jw-0626-jaxb.html
Command Syntax: xjc your.xsd -p com.example.package -d
src/generatedOutputDirectory
XSD to C++:
References:
stackoverflow.com/questions/445905/xml-schema-to-c-classes
www.codesynthesis.com/products/xsd/
XSD to C#:
Reference:
quickstart.developerfusion.co.uk/quickstart/howto/doc/xmlserialization/XSDToCls.aspx
Command Syntax: "C:\Program Files\Microsoft
SDKs\Windows\v6.0A\Bin\x64\xsd.exe" -c -l:c# -n:SomeNameSpace
example.xsd
If the XML was created by JAXB, it can easily be converted back into objects. There's a tutorial over at oracle which illustrates one way to do this. Spring framework offers similiar features using JAXB which are very conveniant.

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!

how could I generate xsd dynamically

I want to generate a xsd file dynamically, I don't want to use jaxb because it creates alot of files when I use schemagen and I want the schema in one file only.
any idea?
If you use a JAXB runtime to generate an XML schema it should produce one Result per namespace URI:
http://wiki.eclipse.org/EclipseLink/Examples/MOXy/JAXB/GenerateSchema

Generating Java classes from XML file

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

Categories