• 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

Java to XML

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can someone tell me how i transfer some int and string values from a Java class into a XML document? Later the XML document have to transfer the values and their contents back to the Java class.
 
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First we need to know the structure of the XML document you are writing to, and whether it is in memory or out on a file syste, in a database etc.
The simplest way is probably to parse a file into a JDOM or DOM Document, add a few elements for your values and write it back to disk. Please let us know a bit more about what you need to do.
 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, I just read that you can't use PCDATA tags for java code, you need to use CDATA tags, right?? Otherwise you can't use <,>,",' or & characters. Is this what you were wondering?
So you can go:
<JAVA><[CDATA[
code goes here, and no attempt is made to parse it as XML
]]></JAVA>
This may or may not be close to reality (or to your question!) I was pretty sleepy this afternoon when I read the chapter!
[guess I'm pretty sleepy now--that wasn't what you asked at all! Oh well]
HTH
[This message has been edited by eric moon (edited December 17, 2000).]
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Frank
If i understand you rigth you want to do a XML - RPC.XML is a good alterative to RMI or CORBA but it has some disadvantages:
1: XML is text so the tranfer can be slow
2. You tranfer XML as plaintext -this can be a Security lack
so u have to tranfer it with HTTPS

so i have a link for you - look at http://www.xmlrpc.org/
hope this helps
Bye,
Holger
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If your doubt is very basic, (ie) how to convert a java object into an xml document, this is what you should do.
1. Write a java program to create a text file (e.g a.xml)using FileWriter (or any low level streams).
some thing like this,
FileWriter fw=new FileWriter(new File("a.xml"));
fw.write("<?xml version=\"1.0\"?>");
fw.write("<Name>" + obj.Name + "</Name>");
You will have to write another java program to parse this document using a parser like "xerces" (from http://xml.apache.org) and create the object from the parsed values.
---
If your doubt is about how to differentiate between an int and a string, since everything is treated as text in an xml document,
you could use attributes in the xml document.
For Example,
<Address>
<Name type="String">
John Smith
</Name>
<Phone type="int">
1800800800
</Phone>
</Address>

[This message has been edited by Alagu Seenivasan (edited December 20, 2000).]
 
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am not sure which package u are using. But if u are using JAXP. there is an object called XmlDocument, here is the api for JAXP. http://java.sun.com/xml/docs/api/internal/overview-summary.html
U should find the link to download in java's xml section.
Also, Sun is working on a project called JAXB, data binding. probably will do the similiar thing, but too bad it is not out yet.
You can create a XmlDocument, append node and text, then write it out to a file.

[This message has been edited by Cynthia Yao (edited December 21, 2000).]
 
Bartender
Posts: 783
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Frank Maier,
Give us an example of your XML document and we should be able to come up with some examples of how to do it for you.
-Peter
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
why not domify? it is a very nice and smart adapter of transfering javabean to xml.
http://domify.sourceforge.org
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic