aspose file tools*
The moose likes XML and Related Technologies and the fly likes DOM code generator for XML 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 "DOM code generator for XML" Watch "DOM code generator for XML" New topic
Author

DOM code generator for XML

sh. garg
Greenhorn

Joined: Jan 21, 2009
Posts: 10
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
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1282

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
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42602
    
  65
What do you mean by "DOM code for that xml"? DOM is just an API - what should the code do?


Ping & DNS - my free Android networking tools app
sh. garg
Greenhorn

Joined: Jan 21, 2009
Posts: 10
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

Joined: Jan 21, 2009
Posts: 10
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
Bartender

Joined: Mar 07, 2007
Posts: 1282

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

Joined: Jan 21, 2009
Posts: 10
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
Bartender

Joined: Mar 07, 2007
Posts: 1282

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

Joined: Jan 21, 2009
Posts: 10
Thanks a ton!!



Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1282

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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: DOM code generator for XML