Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / March 2006

Tip: Looking for answers? Try searching our database.

Creating XML Document

Thread view: 
bwilde@gmail.com - 30 Mar 2006 17:27 GMT
Hello,

I am trying to use Java's XML capabilities to write a new XML file.
All of the examples that I have seen either use the DOM to import an
existing XML file, or ignore a DOM and create an XML file by simply
writing statements line by line.

My XML file is going to be pretty complex with a fair amount of
children under each node.  I would like to keep track of it as a DOM,
which should make it easy to append children, then simply export it at
the end.  Is there a way to do this?  I still haven't been able to
simply create a new, blank document and assing it a root node.  I
imagine that once I have a document with a root node, everything else
would follow pretty easily.

Thanks,
Ben
Oliver Wong - 30 Mar 2006 18:21 GMT
> Hello,
>
[quoted text clipped - 10 lines]
> imagine that once I have a document with a root node, everything else
> would follow pretty easily.

Googling for "Java DOM create blank XML" (without the quotes) gives me:
http://www.roseindia.net/xml/dom/
<abstract>
This tutorial shows you how to create blank DOM document.
[...]
In the next section I will show you how to add root and child elements to
the blank document. Saving the DOM tree to the disk file is also discussed
in the next secion.
</abstract>

   - Oliver
Steve W. Jackson - 30 Mar 2006 18:58 GMT
> Hello,
>
[quoted text clipped - 13 lines]
> Thanks,
> Ben

You'll want to look more closely at the API.  At its simplest, you can
use a DocumentBuilderFactory to create a DocumentBuilder, which can then
create a Document via its newDocument() method.  On the Document,
createElement and then appendChild can be used to create your root
element and add it.  Depending on your version of Java, you can find
multiple ways to serialize that document to a file.  In 1.4.2, I've used
a TransformerFactory to get a Transformer, set its properties, and then
transform a DOMSource (your document) into a StreamResult (an output
file, with encoding specified).

I haven't used 1.5 and don't yet know what else it may offer to improve
on this, but I do know that the classes I've named here are all present
and should still work just fine.

= Steve =
Signature

Steve W. Jackson
Montgomery, Alabama



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.