aspose file tools*
The moose likes XML and Related Technologies and the fly likes Convert XSD to JAVA Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Convert XSD to JAVA" Watch "Convert XSD to JAVA" New topic
Author

Convert XSD to JAVA

ram shyam
Ranch Hand

Joined: May 04, 2007
Posts: 77
Hi all,

I would like to convert a xml schema file (.xsd) to corresponding JAVA objects without using any 3pps like JAXB or XMLBeans. I can probably write down the POJO (getters and setters) for each element/attribute corresponding to the xsd file. But, without using any of the either JAXB APIs or XMLBeans APIs, how can i parse the xml document contents to the POJO that I have created earlier?

Please let me know if this is possible or any other means to achieve the same.

Thanks in advance!!
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12835
    
    5
without using any of the either JAXB APIs or XMLBeans APIs


Where did that restriction come from? Is this some kind of homework assignment?

Anyway - since the XSD document is just text, you can parse it with typical String methods - a bit of a pain but do-able.

Bill
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18991
    
    8

It seems that you are allowed to use Java parsers? Then the existence of things like JAXB, which are Java code bases which map schemas into Java objects, clearly and trivially shows that it is possible to write Java code which maps schemas into Java objects.

So yes, it's possible. But probably that wasn't really your question, was it?
ram shyam
Ranch Hand

Joined: May 04, 2007
Posts: 77
Hi all,

As per one of my project requirements, in which we do not have the license agreement to add any 3pps to the existing product, I want to convert a XSD to POJO. Since the project is built on jre1.4 version, JAXB jars cannot be shipped as per the license agreement. If it had been jre1.6 version, then the jaxb APIs could have been implicity used and the unmarshalling can be done with ease.

So, in this scenario, as I had mentioned earlier, I can very well code the getters and setters using JAVA Beans for each element defined in the XSD file. But once this is done, how will I parse the xml document contents using the getters that I have created earlier. Ofcourse, I know that there exists the JAXB, XMLBeans APIs to unmarshall the xml document to JAVA objects. But without using these APIs, is there any means to get this done?
Please clarify.

Thanks in advance!!
Balu Sadhasivam
Ranch Hand

Joined: Jan 01, 2009
Posts: 874

how will I parse the xml document contents using the getters that I have created earlier


Using the possible combination of JAXP (inbuilt in JDK1.4) and Reflection.

1) Parse the XML using JAXP DOM parsers and get the node name for nodeType.
2) Use reflection to get the Bean Class and set the nodeName to the corresponding setter ( method.invoke() iwth parameters)
3) At the end , you would be having Bean object with xml data.

But all this is easily possible if the XSD is simple and the created setter and getter is a single Bean object. Otherwise 2) point wud be difficult as it has to find the correct Bean calss before it can set it setter. For this may be maintain an xml contains possible bean classes and invoke using it.

Its an idea, try working on it . good luck.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Convert XSD to JAVA