• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

DOM Parser

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Amit Ganatra
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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".
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic