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
Related
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.
I have several XSD files which I have processed using XJC to generate Java classes.
I would like to read an arbitrary XML file, and based on the contents of the XML file, convert it to one of the generated Java class.
Is it possible to do this without examining the contents of the XML itself, and letting JAXB framework do all the work?
For example, let's say I have the following XSD files:
dog.xsd
cat.xsd
I compile them using XJC to generate Dog.java and Cat.java.
I now read some arbitrary XML, and create an instance of either Dog or Cat, depending on which schema it matches.
Thanks.
XJC generates classes based on the types in your schemas, not the schemas themselves. And it puts the generated classes into packages based on that target namespaces. If you load multiple schemas into the JAXBContext, it will automatically unmarshall to the correct type. That's pretty much the whole point of JAXB. If your two schemas have types with the same names in the same namespace, XJC would fail.
A simple solution that might work is try to unmarshall one of them, if it goes ok, then its the right one, if you get an exception that its the second one you should use.
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!
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?
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