This week's book giveaway is in the Mac OS forum. We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line! See this thread for details.
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.
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?
Joined: May 04, 2007
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?
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.