• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

DOM code generator for XML

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ,

I have a lengthy xml for which I need to write DOM code.
Is there some way using which I can give that xml file as input and get a java file containing DOM code for that xml as output.

Regards,
Shobhit
 
best scout
Posts: 1294
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

do you know JAXB? With the JAXB framework you can define a mapping between XML elements and Java classes and JAXB takes care of the marshalling and unmarshalling for you. If you have an explicit schema for you XML documents it's even more easier because JAXB can automatically create the necessary Java classes for the schema.

Of course this is not DOM but in my opinion it's much more handy to use ordinary Java classes with annotations instead of DOM objects.


Marco
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What do you mean by "DOM code for that xml"? DOM is just an API - what should the code do?
 
sh. garg
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Marco Ehrentreich wrote:Hi,

do you know JAXB? With the JAXB framework you can define a mapping between XML elements and Java classes and JAXB takes care of the marshalling and unmarshalling for you. If you have an explicit schema for you XML documents it's even more easier because JAXB can automatically create the necessary Java classes for the schema.

Of course this is not DOM but in my opinion it's much more handy to use ordinary Java classes with annotations instead of DOM objects.


Marco



Hi Marco,
Thanks for the suggestion.I tried jaxb already but unfortunately it seems the XSD has some issues as xjc complains saying "element already defined" and the xsd cannot be changed

Shobhit
 
sh. garg
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ulf Dittmer wrote:What do you mean by "DOM code for that xml"? DOM is just an API - what should the code do?


Hi Ulf,

I need to write some code that generates a xml .Now i am unable to use JAXB.So the option left is DOM api.
Using the DOM api i have to output the xml similar to the xml that i mentioned above.

So I was wondering if there was any utility that would take that template xml as input and return the DOM code to generate that XML.Later I will
tweak the code returned to my specific needs.

Regards,
Shobhit

 
Marco Ehrentreich
best scout
Posts: 1294
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Shobhit,

unfortunately (or luckily ) I only used JAXB for valid XML schemata. Although I understand your problem, it makes perfect sense that XJC is unable to generate classes for an invalid schema. It's just questionable why there exists a schema to validate XML data in the first place when even the schema itself isn't valid

Anyway I think it should be possible to use JAXB (if you want to). I guess your only way is to create the corresponding annotated classes for XML elements by hand and turn off explicit validation if this would fail when marshalling or unmarshalling XML documents. Have you already tried to do it manually? As I said, I never used JAXB for invalid XSDs myself but I'm pretty sure you can still use it even though your schema is invalid.

Marco
 
sh. garg
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Marco Ehrentreich wrote:Hi Shobhit,

Anyway I think it should be possible to use JAXB (if you want to). I guess your only way is to create the corresponding annotated classes for XML elements by hand and turn off explicit validation if this would fail when marshalling or unmarshalling XML documents. Have you already tried to do it manually? As I said, I never used JAXB for invalid XSDs myself but I'm pretty sure you can still use it even though your schema is invalid.

Marco



Hi Marco,

How do I do this ? ie how do I write the annotated classes by hand . I am new to this stuff.
Any pointers would be helpfull.Tried googling but couldnt find anything helpfull.


Cheers
Shobhit
 
Marco Ehrentreich
best scout
Posts: 1294
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Shobhit,

this is relatively easy. First you should use Java >= 1.5 and JAXB 2.x in order to use annotations.

Then you should create classes corresponding to the elements you want to retrieve from your XML documents. This doesn't have to be a 1:1 mapping. The classes can be simple POJO classes. For example if you have an XML element for a "car" you most probably want to create a Java class "Car" which contains the data and methods belonging to a car. That simple. No special API classes to inherit from. No specific interfaces or methods to implement.

The rest of the process is completely based on ordinary annotations. JAXB has quite a good default behavior regarding the mapping between XML elements and types and Java types. But you still have to tell the marshaller and unmarshaller of the JAXB framework how to transform data between your Java code and XML documents. For the example above this means you have to annotate the class "Car" accordingly so that JAXB knows that the class "Car" is mapped to the XML element "car". It's too much to list all annotations and their attributes here but you'll find enough information in the internet. In particular have a look at Sun's web services tutorial. It explains the entire JAXB architecture and API. The "Java-to-schema" sections should give you a good understanding about how to use the JAXB annotations.

I hope this helps ;-)

Marco
 
sh. garg
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a ton!!



 
Marco Ehrentreich
best scout
Posts: 1294
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome ;-)

Just give it a try with JAXB. I used it recently for a project and I was very happy with it! If you're stuck with something feel free to ask!

Good luck!


Marco
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic