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 / February 2006

Tip: Looking for answers? Try searching our database.

Transforming DOM to file strips out DOCTYPE element

Thread view: 
O.B. - 06 Feb 2006 19:27 GMT
The code below simply reads in an XML file and prints it back to to a
file.  Unfortunately, it drops the !DOCTYPE element.  Any clue why this
is happening?

import java.io.IOException;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;

Document doc = null;
try {
  // Create a builder factory
  DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  factory.setValidating(true);
  // Create the builder and parse the file
  Document doc = factory.newDocumentBuilder().parse(new File(filename));
} catch (SAXException e) {
  // A parsing error occurred; the xml input is not valid
} catch (ParserConfigurationException e) {
} catch (IOException e) {
}

// At this point, I have verified that doc.getDoctype points
// to valid data.

// Write data in memory (XML Document) to output stream
FileOutputStream tfos = new FileOutputStream("temp");
Result result = new StreamResult(tfos);
Source source = new DOMSource(hibernateDoc);
try {
  Transformer xformer = TransformerFactory.newInstance().newTransformer();
  xformer.transform(source, result);
} catch (TransformerConfigurationException e) {
  e.printStackTrace();
} catch (TransformerFactoryConfigurationError e) {
  e.printStackTrace();
} catch (TransformerException e) {
  e.printStackTrace();
}
tfos.close();

// At this point the "temp" file does not have a DOCTYPE element.
O.B. - 06 Feb 2006 19:31 GMT
PS Here's the DOCTYPE I'm trying to preserve:

<!DOCTYPE hibernate-configuration SYSTEM
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

> The code below simply reads in an XML file and prints it back to to a
> file.  Unfortunately, it drops the !DOCTYPE element.  Any clue why this
[quoted text clipped - 42 lines]
>
> // At this point the "temp" file does not have a DOCTYPE element.
Damian Brunold - 07 Feb 2006 10:14 GMT
> PS Here's the DOCTYPE I'm trying to preserve:
>
[quoted text clipped - 4 lines]
> > file.  Unfortunately, it drops the !DOCTYPE element.  Any clue why this
> > is happening?

You might want to try

transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM,
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd");

See


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



©2009 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.