• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Convert XSD to JAVA

 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 28328
96
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Seriously Rick? Seriously? You might as well just read this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic