aspose file tools*
The moose likes XML and Related Technologies and the fly likes DOM Parser 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 Parser" Watch "DOM Parser" New topic
Author

DOM Parser

Amit Ganatra
Greenhorn

Joined: Jun 19, 2001
Posts: 19
Hello Friends,
I am using a DOM Parser in my application to read data from an XML file. At times i have to write back to the same XML file but cant do so becuse writting back to the XML file using the DOM methods only modifies the DOM tree in memory and not the actual document.
What could be the way to copy the modified tree (in the memory) back to same XML file without loosing any inentations ?
Thanx,
amit
------------------
Sun Certified Java Programmer


SCJP2
Ajith Kallambella
Sheriff

Joined: Mar 17, 2000
Posts: 5782
JAXP1.1 makes this a simple process. JAXP allows any XML Parser and any XML Transformer(such as an XSLT Engine) to be used using the JAXP API methods.
Each instance of the class javax.xml.transform.Transformer can be associated with an .xsl file, but it can have no association. javax.xml.transform.Source and javax.xml.transform.Result allow you to transform a Source object into the Result object. Both the Source and Result objects can be used to represent a org.w3c.dom.Node object (can be a Document), or a Stream type object (file, writer, stream).
By creating a Transform object without an underlying .xsl file, you can transform a Source(the internal DOM object) to a StreamResult(created from a file). This will write the DOM object to the file.
Transformer objects are created from the TransformerFactory object. The TransformerFactory object uses system properties to determine which parsers to use, allowing you to use this API with any JAXP parser. JAXP includes the Crimson parser and Xalan Transformer.
JAXP1.1 contains a few examples which help make the process easy to understand
Using last version of Xalan-Xerces all you have to do is to instanciate a new transformer (using the no parameter constructor) . That will instantiate a Transformer that transforms nothing, just copy source to destination.


You do not have to browse the whole DOM !

Using Xerces

using Crimson
Using the Sun's Crimson you can cast the w3c document to type of XmlDocument and then use the write method.

This method works except for a small shortcoming - every save creates a carriage return character behind every closing tag. As I open modify and then save the same file these characters are adding up quickly.

Using Transformer Package that is included with JAXP1.1

This method works great except for one major flaw. If your document had a DOCTYPE, when you load your document, add some nodes etc.. and then save, you will notice the DOCTYPE declaration missing!!.
Hope that helps!

------------------
Ajith Kallambella M.
Sun Certified Programmer for the Java�2 Platform.
IBM Certified Developer - XML and Related Technologies, V1.


Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
Amit Ganatra
Greenhorn

Joined: Jun 19, 2001
Posts: 19
Hi Ajith,
I tried using XML Serializer code that u have attached in ur reply. The code works fine and it copies all the changes back to the XML file. Thanx for ur help.
The only problem with this code is, the new file has all elemnts copied in a single line. Can u suggest me a way by which the old file and the new file look exactly alike i.e. the new file should have same indentations as the old one.

Thanx,
amit
Originally posted by Ajith Kallambella:
[B]JAXP1.1 makes this a simple process. JAXP allows any XML Parser and any XML Transformer(such as an XSLT Engine) to be used using the JAXP API methods.
Each instance of the class javax.xml.transform.Transformer can be associated with an .xsl file, but it can have no association. javax.xml.transform.Source and javax.xml.transform.Result allow you to transform a Source object into the Result object. Both the Source and Result objects can be used to represent a org.w3c.dom.Node object (can be a Document), or a Stream type object (file, writer, stream).
By creating a Transform object without an underlying .xsl file, you can transform a Source(the internal DOM object) to a StreamResult(created from a file). This will write the DOM object to the file.
Transformer objects are created from the TransformerFactory object. The TransformerFactory object uses system properties to determine which parsers to use, allowing you to use this API with any JAXP parser. JAXP includes the Crimson parser and Xalan Transformer.
JAXP1.1 contains a few examples which help make the process easy to understand
Using last version of Xalan-Xerces all you have to do is to instanciate a new transformer (using the no parameter constructor) . That will instantiate a Transformer that transforms nothing, just copy source to destination.


You do not have to browse the whole DOM !

Using Xerces

using Crimson
Using the Sun's Crimson you can cast the w3c document to type of XmlDocument and then use the write method.

This method works except for a small shortcoming - every save creates a carriage return character behind every closing tag. As I open modify and then save the same file these characters are adding up quickly.

Using Transformer Package that is included with JAXP1.1

This method works great except for one major flaw. If your document had a DOCTYPE, when you load your document, add some nodes etc.. and then save, you will notice the DOCTYPE declaration missing!!.
Hope that helps!
[/B]


------------------
Sun Certified Java Programmer
Joseph Rank
Greenhorn

Joined: May 03, 2001
Posts: 7
Amit,
Did you try using the XMLSerializer constructor with the OutputFormat object as an arg? The OutputFormat class supports two 3-arg constructors which take a boolean as the 3rd argument that controls indenting.
NB: the OutputFormat.Defaults inner class defines a LineWidth field that can add line breaks into your XML content. Such is the price of "pretty printing".
ak amanushya
Greenhorn

Joined: Aug 20, 2001
Posts: 7
Hello,
The requirement is that the characters in czech is supposed to be read and with no manipulation it has to be printed to thrown to the browser.
I am using Jaxp, Crimson parser's document.parse method to parse the doc. "Document xmlDocument = DocBuilder.parse(xmlFile);" - xml file is a statc file containing the xml content in czech. The problem is that, immediately when I try to write the characters read to another file, the characters seem to be corrupted.
Question is how these spcl chars need to be handled..? It will be highly appreciable if someone can come up with any suggestion inluding if it is a limitation with Crimson or with any method to solve this.
Many Thanks
ak
mints yang
Greenhorn

Joined: Mar 06, 2006
Posts: 1
When I use Crimson for output dom tree

XmlDocument newdoc = (XmlDocument) doc;
newdoc.write(new FileWriter(fileNameToWrite),"ISO-8859-1");

I got an error in classcast from Document to XmlDocument? Can you help me on it?

Best Wishes!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: DOM Parser