aspose file tools*
The moose likes XML and Related Technologies and the fly likes DOM and JAVA Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "DOM and JAVA" Watch "DOM and JAVA" New topic
Author

DOM and JAVA

oLi Bou
Greenhorn

Joined: Jul 03, 2002
Posts: 23
hi,
i would like to find a way to save a DOM tree making a new XML file.
in fact, i load an XML file, remove some nodes in DOM's way and i wish i could save my modified tree in another (or maybe the same) XML file .. i know it could be easyer using XSLT but i'd like to know anyways
thx


--learn every day--
Ajith Kallambella
Sheriff

Joined: Mar 17, 2000
Posts: 5782
You could use XMLSerializer to first convert the document to a String format.

In this example, a String equivalent of the XML document is being returned to you. You can take this method and easily tweak it to write to a file.
Hope that helps.


Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
Ajith Kallambella
Sheriff

Joined: Mar 17, 2000
Posts: 5782
Your name axe- does not comply with the JavaRanch naming policy. We require the displayed names to have at least two words, separated by a space, and strongly recommend that you use your full real name. Please spare a moment and modify your profile to use a publicly displayed name that meets these requirements.
Thanks!
oLi Bou
Greenhorn

Joined: Jul 03, 2002
Posts: 23
thx
can you say me wich packages i must import ...
and ..
[another point .. along the app i use Node instead of Document (coz i use stuff like removeChild() and so on ).. how is it possible to reuse my "root" node as a document ?? ]
for that part i have found .. i am a real newbie to Java so i didn't even knew 'bout downcasting

[ July 03, 2002: Message edited by: oLi D ]
Ajith Kallambella
Sheriff

Joined: Mar 17, 2000
Posts: 5782
import java.io.StringWriter;
import org.apache.xml.serialize.XMLSerializer;
import org.apache.xml.serialize.OutputFormat;
import org.apache.xerces.dom.DocumentImpl;
import org.apache.xerces.dom.DocumentTypeImpl;
import org.apache.xerces.dom.DOMImplementationImpl;
You should have a reference to the Document somewhere in your program. This is what the parse() method returns and that would be your pointer to the root.
oLi Bou
Greenhorn

Joined: Jul 03, 2002
Posts: 23
thx BTW, it really worked nice ... after i solved the problem of importing de packages ..
see ya
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
I learned about DOM in an XML class and I did an assignment or two with it, but when I decided to develop a mock exam in XML format, I used JDOM instead and found it to be extremely easy to use since the document is stored using the Java Collections Framework.
The web site contains four comprehensive exams. Each of the four has 22 questions for a total of 88. The same set of 88 questions are also presented as a set of 8 topic specific exams.

All of the questions have been developed in a set of XML documents--one for each topic. Using JDOM (with some help from Xerces) the questions are loaded into memory and distributed randomly between four comprehensive exam documents. While the documents are still in memory they are shuffled and then numbered. After the documents are written out to a new set of four xml files, Xalan is used to transform each of the four into two html documents--one for the questions and one for the answers. The result is eight html files. CSS is used to put the final touches on the presentation.
If you have an opportunity to give JDOM a try, then I would certainly recommend it.


Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: DOM and JAVA