How do you include a DTD when generating an XML document

Hi
I have just started to use the Java XML Pack to generate a XML document.
How do you put the dtd element
<!DOCTYPE Controller SYSTEM "controller.dtd">
in the generated document.
I am using the following piece of code
public void generate(...)
DocumentBuilder builder = factory.newDocumentBuilder();
document = builder.newDocument();
Element rootElement = createElement("Controller");
// Fill out the nodes etc
// Save the result
TransformerFactory tFactory =
TransformerFactory.newInstance();
Transformer transformer = Factory.newTransformer();
File f = new File("dummy.xml");
FileWriter fw = new FileWriter(f);
DOMSource d1 = new
DOMSource(rootElement,"controller.dtd");
StreamResult result = new StreamResult(fw);
transformer.transform(d1, result);
Thanks
Greg

Hello,
I had to go through quite some pain to get this done about a year ago,anyways,here is the solution.
all the import files are in jaxp1.1 bundle.
below is a test class code.
import org.apache.crimson.tree.XmlDocument;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import java.io.*;
public class CreateDTDInXMLFile {
public CreateDTDInXMLFile() {
public static void main(String[] args) {
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.newDocument();
Element root = document.createElement("root");
Element name = document.createElement("name");
name.appendChild(document.createTextNode("Khalid"));
Element address = document.createElement("address");
address.appendChild(document.createTextNode("2022 11th Ave SW"));
root.appendChild(name);
root.appendChild(address);
document.appendChild(root);
((XmlDocument)document).write(System.out);
//the above will print the simple xml doc,now add the dtd
//suppose the dtd is [<!DOCTYPE root SYSTEM "address.dtd">]
((XmlDocument)document).setDoctype(null,"address.dtd",null);
//now print the xml doc again
System.out.println("\n");
((XmlDocument)document).write(System.out);
} catch(ParserConfigurationException pce) {
pce.printStackTrace();
return;
}catch (IOException ioe) {
ioe.printStackTrace();
This will take care of the agony..
Khalid

Similar Messages

  • How can i use a xsd while generating my xml document in oracle?

    Hi, how can i use a xsd while building my xml document query, so i know the xml was built properly?
    Cheers.

    Hi,
    Do you want to validate your xml against xsd?
    Read this
    validating
    There are some bugs also, for example  xml is invalid but no errors when calling schemaValidate()
    Ants

  • HT1689 how do you unlock your ipod when its disabled?

    how do you unlock your ipod when its disabled?

    This is the same thing:
    Place the iOS device in Recovery Mode and then connect to your computer and restore via iTunes. The iPod will be erased.
    iOS: Forgot passcode or device disabled
    If recovery mode does not work try DFU mode.                        
    How to put iPod touch / iPhone into DFU mode « Karthik's scribblings        
    For how to restore:
    iTunes: Restoring iOS software
    To restore from backup see:
    iOS: Back up and restore your iOS device with iCloud or iTunes       
    If you restore from iCloud backup the apps will be automatically downloaded. If you restore from iTunes backup the apps and music have to be in the iTunes library since synced media like apps and music are not included in the backup of the iOS device that iTunes makes.
    You can redownload most iTunes purchases by:
    Downloading past purchases from the App Store, iBookstore, and iTunes Store        
    If problem what happens or does not happen and when in the instructions? When you successfully get the iPod in recovery mode and connect to computer iTunes should say it found an iPod in recovery mode.
    If you previously turned on FIndMyiPod on the iPod in Settings>iCloud and wifi is on and connected go to iCloud: Find My iPhone, sign in and go to FIndMyiPhone and use Remote Wipe to erase the contents of your device. If you have been using iCloud to back up, you may be able to restore the most recent backup to reset the passcode after the device has been erased.
    You can also wipe the iOS device by installing the FindMyiPhone app on another iOS device and using that app to wipe the device.
    Just what do you not understand?

  • In Lion, how do you bypass managed prefs when logging on?

    in Lion, how do you bypass managed prefs when logging on?

    Ahh, that's a Lion Server question...sorry, can't help.
    Someone else hopefully will be along to answer for you.
    EDIT: won't help you in the short-term, but long term might be worth investing in this:
    http://shop.oreilly.com/product/0636920022664.do

  • How can you download premier pro when the icon doesn't exist in cloud?[was:pr]

    how can you download premier pro when the iucon doesent exist in cloud???

    In order to download the software, your computer must meet the system requirements to run the software.
    http://helpx.adobe.com/x-productkb/policy-pricing/system-requirements-premiere-pro.htmlhttp://www.adobe.com/products/premiere/tech-specs.html
    The usual reason for not being able to download is that Premiere Pro (and other video apps such as After Effects) are 64 bit only so they require a 64 bit computer and a 64 bit operating system.
    If you're running a 32 bit computer and operating system then you will not see the Premiere Pro icon since the software will not run on your computer.

  • HT201210 How do you unlock an iPod when you've forgotten the password?

    How do you unlock an iPod when you've forgotten the password?

    Do as the manual says to do and restore the iphone.
    If you are unable to restore, then type "unable to restore" into the search bar at the top of this page by Support and read the resulting help article

  • HT1577 How do you contact I tunes when you have purchased a song and it did not down load?

    How do you contact I tunes when you purchased a song and it did not down load to your library?

    1. Go to expresslane.apple.com
    2. select 'itunes' and then 'itunes store' on the right
    3. select a topic and a subject
    4. click 'continue' and then 'email'
    5. fill out the form and explain your situation
    6. they will respond within 24 hours usually
    Good luck
    Stijn

  • How do you detect url redirects when connecting to a URL

    how do you detect url redirects when connecting to a URL?
    Thanks in advance
    stev

    Hai
    (i) If it a jsp we can get the URL by request object.
    Like
    String url = request.getHeader("Referer");
    u can do it in Servlet aslo by request object variable.
    If this explanation is not ok, Explain ur doubt in more briefed Manner

  • How do you unlock a phone when you dont know the password?

    how do you unlock a phone when you dont know the password?

    Connect in recovery mode and restore, you'll get the option to reset the passcode during this process:
    iOS: Unable to update or restore and iPhone and iPod touch: Wrong passcode results in red disabled screen
    If you cannot remember the passcode, you will need to restore your device using the computer with which you last synced it. This allows you to reset your passcode and resync the data from the device (or restore from a backup). If you restore on a different computer that was never synced with the device, you will be able to unlock the device for use and remove the passcode, but your data will not be present. Refer to Updating and restoring iPhone, iPad and iPod touch software.

  • How do you add a Comment when editing document in Word for ipad?

    How do you add a Comment when editing a document in Word for ipad. I've subscribed to Office, so have full editing functions. Can Track changes etc in Review, but can't see how to add a marginal Comment.

    Comments, like they are in Numbers for OS X, are not really supported in iOS. Normally, they rely on a mouse-over to read them. Since there is no mouse on the iPad, it stands to reason they are not there.
    I believe Eric Ross's comments is more geared towards used said Numbers docs on iWork.com.

  • How do you updated CS5 Extended when you keep getting a message to close the program but it is close

    How do you updated CS5 extended when you keep getting a message to close the program but the program is already closed?  I have tried to do the update when I first turn the computer on, go directly to Photoshop, go to update and click on that, get new screen, close Photoshop, and I still get a message to close the already closed program.  I believe I have to open Photoshop to get to the update area, so I don't know how to get around this issue.  I hope someone can help me.
    (I use win8, 64 bit)
    Thank you in advance to all who answer my request for help.
    Ann

    Thank you for answering.  However, when I opened the Task Manager and looked at all the processes that were running, the only things running were the Task Manager and the browser.  None of the Photoshop programs were running.  So, do you have any other suggestions?

  • How do you get your mouse when rolled to zoom in and out???

    how do you get your mouse when rolled to zoom in and out???

    What operating system? What kind of mouse?

  • How do you fix the ipod when its not playing the music right when it makes noise

    How do you fix the ipod when its not playing the music right when it makes noise

    Hi Nicole,
    Try resetting it (nothing will be lost): Hold down the Home and Power buttons at the same time and continue to hold them down until the Silver Apple appears (up to 30 seconds). The Home screen should redisplay after the reset is complete.
    Cheers,
    GB

  • I deleted the picture in iphoto, then emptied my iphoto trash, but the picture is still showing on my computer when i go into my trash under "all images" How do you delete pictures even when you empty the trash and they stay in the computer?

    I deleted the picture in iphoto, then emptied my iphoto trash, but the picture is still showing on my computer when i go into my trash under “all images” How do you delete pictures even when you empty the trash and they stay in the computer?

    Wtih iPhoto 9 when the iPhoto Trash is emptied the files get moved to the Trash bin in the Dock as a second chance to change your mind.  Look in the Dock's Trash bin.
    OT

  • How do you fix error -3256 when trying to put ios 5 on my phone?

    How do you fix error -3256 when trying to put ios 5 on my phone? I have a mac. I have backed up everything....?

    I always get the same error 3256 (as Timothy98) when trying to download ios 5 on my upgraded iTunes in my macbook pro. Please help. I want to start using ios 5.

Maybe you are looking for

  • How do I de-authorise if it's multiple times on one computer?

    Recently my 2003 iPod died and my nano was recalled for a battery problem. On both of these, I had a book that I had purchased through iTunes. It's a very large book (3 large volumes) and I cannot back it up to an audio cd. It has once more asked me

  • TV out help

    Hello, please, I have a big problem with the tv out. I have my screen but in black&white :( thanks to anyone who can describe me in every step how to have my Mega180 on my tv (pal). what I have to modify in bios ? if someone can take screenshot it wi

  • Videos to Itunes/Ipod classic 80GB

    Is there anyway to upload tv shows and movies to Itunes or the Ipod 80GB classic if they didnt come from the itunes store?

  • Basic query on PLSQL using TOAD

    hi all, First of all thanks a lot to everyone for always helping and guiding me through all the doubts i had asked here.I have leasrned a lot and appreciate your help. So,my question is executing the STORED PROCEDURE in Toad 9.0 execute assign_daily_

  • Computer died, how do i transfer my playlist

    My computer has died and I can no longer access my playlist. How can I transfer my music to a new computer?