This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Building Blockchain Apps and have Michael Yuan on-line!
See this thread for details.
Win a copy of Building Blockchain Apps this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Liutauras Vilda
  • Knute Snortum
  • Bear Bibeault
Sheriffs:
  • Devaka Cooray
  • Jeanne Boyarsky
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • salvin francis
Bartenders:
  • Tim Holloway
  • Piet Souris
  • Frits Walraven

Convert XSD to JAVA

 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!!
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
Marshal
Posts: 25223
65
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!!
 
Ranch Hand
Posts: 874
Android VI Editor Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
They worship nothing. They say it's because nothing is worth fighting for. Like this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!