• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Creating XML from scratch

 
Ranch Hand
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello everyone... I need to know what's the simplest way to create an XML document using java. The xml document should be well formed. For example, if I have the data, how do I put everything between their respective tags? I've read some topic posted before but I didn't quite understand how to do it. are there any resources available that I can have a look at? (URL's, source code)
thanks in advance
 
Saloon Keeper
Posts: 25482
180
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Probably the simplest way to generate XML in Java is to use one of the packages that will take a Java class and output all its fields. I'd have to do some websearching to tell you their names, though.
For more general XML output, I have a class called XMLWriter which allows you to write arbitrary tags and data. You can find it in the source code for the EJBWizard at http://www.mousetech.com/EJBWizard.html. All you need is the one source module (XMLWriter.java).
Let me know if you have download problems. The servers were recently worked on and sometimes things I thought were working weren't.
The source file is in "tar/gzip" format. If you're using Windows, WinZIP can handle that.
[ February 05, 2002: Message edited by: Tim Holloway ]
 
Andres Gonzalez
Ranch Hand
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Tim... I've downloaded the EJBWizard-3.1.2 - Near-production and didn't have any problem, it works pretty well. Now, I've checked the XMLWriter.java and it seems to generate the XML file according to the data stored in a vector, right? Anyway, i have to spend more time using it. Another question: Should I use the EJBWizard 3.4 to create my XML file? I was planning to install Jboss 'cause I need to create some entity or session bean (don't know yet) that can handle the XML file creation, so this tool could help me a lot ...
thanks
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I usually use the JDOM API (www.jdom.org). It makes it very easy to read/write/modify XML, but of course uses quite a lot resources. So, if you have a "real-time" application, I would not recommend it (but who would use Java for this anyway...).
 
Tim Holloway
Saloon Keeper
Posts: 25482
180
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
XMLWriter is designed for manual control of the XML creation process. You can generate tagged data using the startTag/endTag methods like so:

You can create multi-level XML like so:

Which wasn't the best way maybe, but it works.
For a simpler output, use printTag( tag, value ).
The printTag( vector, topic, tag ) was a convenience when I wanted to put out lists where each item was a tagged item under a topic.
You can also start a tag with attributes using startTag( tag, attributes ) where attributes is an array of objects in pairs like:

The EJBWizard is a GUI tool where you fill in data to define the Enterprise JavaBean you wish to construct and then do a File/Generate menu command to cause the prototype EJB code and infrastructure (JSP's, Ant buildfile, etc. to be created. It's template driven to be easily extensible, but if what you're looking for is to output XML from an app you write, the XMLWriter class is the only one you need. The EJBWizard uses XMLWriter as a utility class to output a project (EJB definition) in XML format.
It's a good idea to escape your data when writing XML. In the example up top I used the print() method that XMLWriter inherits from printWriter, but there is also a printQuotedTag method as well as a static method that will take a string and XML-escape it. Or you can manually generate CDATA blocks.
 
CAUTION! Do not touch the blades on your neck propeller while they are active. Tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic