• 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
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

dataobjects to xml document

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
I have an array of dataobjects(java classes), coming in, and i have to extract the data from these objects and create a songle document.
for ex:
I have a class Customer with name,phone,social security and address. I have to create a document with all these tags
and fill in the information brought in by the data object. Is there any easy way of doing this???
please help
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could write toXML() method on your class that builds the XML string. It should use attribute names as tags and attribute values as the content.

You can then build the final XML document by first creating the root element and then calling the toXML() on all of your object concatinating the string returned by the method and finally by closing the root element.
Hope that gives you something to think about
------------------
Ajith Kallambella M.
Sun Certified Programmer for the Java�2 Platform.
IBM Certified Developer - XML and Related Technologies, V1.
[This message has been edited by Ajith Kallambella (edited February 22, 2001).]
 
slagy maggie
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
That definetly gave me a good insight...but i have a slight problem...the class fields dont match my xml tags for eg
we get only lastname and firstname in the customer class,but my xml string is
<name>
<first>rupa</first>
<last>kalidoss></last>
</name>
the program should be smart enough to insert the name tag, and also maintain the hierachy(class customer is flat),i am thinking the only way of doing this is somehow refrencing the dtd for customer,and there by building a document and then outputting it back as a string. But i have no clue as to how to reference the dtd,use the values in dataobjects and eventually build a document.Your help is greeatly appreciated.
 
Ajith Kallambella
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmmm..that is going to be tricky.
Well for one thing, parsing the DTD( for that matter even the XMLSchema ) and to generate an empty XML document is not going to be easy. Remember one DTD( or Schema ) can actually be valid for more than one XML document when all the grouping and mixing of content is allowed. This means the mapping between a definition to an acutual document is not one-to-one, but one-to-many.
I just can't think of any elegant way to do this. You will have to hardcode the logic in the toXML() method( see my previous answer ) to actually create the hierarchical structure in the XML document.

This will do the job. However you have to make sure that you update this method for every change you do for the DTD/Schema.
There's hope on the horizon. Sun has proposed a specification for XML databinding which is in the community process phase now. Once mature, this allows you to seamlessly "cook" classes from an XML document and vice-versa. They will initially support only DTDs and there has been no comment on the Schema support. Check it out!
Good luck,
------------------
Ajith Kallambella M.
Sun Certified Programmer for the Java�2 Platform.
IBM Certified Developer - XML and Related Technologies, V1.
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I recently used Castor, an open-source product from Exolab (castor.exolab.org). Working from an XML schema, Castor converts Java objects to XML and XML to Java objects, without you having to write any code!
I used Castor to create my Java classes - I didn't have existing Java classes to start with, so Castor may or may not work for you. It's worth checking out.
Roger E. Rhoades
------------------
 
rrhoades
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just read about a Java object to XML converter that may help you. The following link contains links to additional Java to XML converters.
http://www.wutka.com/jox.html
Hope this helps.
Roger E. Rhoades
------------------
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roger,
Your name does not comply with the JavaRanch naming policy. Please choose one that meets the requirements. We would like to get to know you as a professional.
Roger Rhoades would be nice.
 
slagy maggie
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ajith...i finished my coding using your way, of building the xml string, now i am required to do the same thing, by building an empty document, and inserting those tags in.....i am using jaxp1.1, could you tell me how i can do this
<name>
<last>slag</last>
</name>
and moreover which method is better as far as performance goes
building an xml string and making it into a doc or
creating an empty doc and inserting tags???
 
Ajith Kallambella
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you mean by "creating an empty doc and inserting tags???" - a DocumentFragment using DOM??
 
slagy maggie
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I mean that you build a new instance of a document
by building docBuilder.newDocument()
and then inserting tags
doc.appendChild("root")....
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!