This discussion is archived
3 Replies Latest reply: Nov 9, 2006 2:50 PM by DrClap RSS

creating a new XML file using DOM?? plzzz help

807607 Newbie
Currently Being Moderated
i want to create a new XML file from an existing DOM tree
i used this code to create a new document:
 static public Document createDocument(String fileName) throws ParserConfigurationException//,IOException,SAXException
     {
          
          try {
               DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
               factory.setIgnoringComments(true);
               factory.setIgnoringElementContentWhitespace(true);
               factory.setValidating(true);
               DocumentBuilder builder =factory.newDocumentBuilder();
               return builder.newDocument();
          }
//          handle exception creating DocumentBuilder
          catch ( ParserConfigurationException parserError ) {
                    throw new ParserConfigurationException();
                    
          }
then i used this code to transform the DOM :
 public void exportDocument(Document document) {

          try {
               Source xmlSource = new DOMSource( document );
     
               Result result = new StreamResult( System.out );
     
               TransformerFactory transformerFactory =
                    TransformerFactory.newInstance();
     
               Transformer transformer =transformerFactory.newTransformer();
     
               transformer.setOutputProperty( "indent", "yes" );
     
               transformer.transform( xmlSource, result );
          }
       //then catching the exceptions
     }
But the file was not created and i didn't find where can i specify the DTD that the XML file should use and where can i enter the name of the XML file itself
Another questoin can i write a DTD file dynamically during the execution of the program??
  • 1. Re: creating a new XML file using DOM?? plzzz help
    DrClap Expert
    Currently Being Moderated
    transformer.setOutputProperty( OutputKeys.DOCTYPE_PUBLIC , "something" );
    transformer.setOutputProperty( OutputKeys.DOCTYPE_SYSTEM , "something" );
    You can't use Transformer to write out an internal DTD.
  • 2. Re: creating a new XML file using DOM?? plzzz help
    807607 Newbie
    Currently Being Moderated
    do you mean that the DTD should exist and can't be created dynamically?


    how can i create the new XML file and give it its name?
  • 3. Re: creating a new XML file using DOM?? plzzz help
    DrClap Expert
    Currently Being Moderated
    do you mean that the DTD should exist and can't be
    created dynamically?
    That would be the sensible thing to do. In my opinion having a computer program produce a document with an internal DTD is rather pointless. Just have the program produce a document that is valid according to the DTD and leave out the DTD entirely. Unless you're declaring entities in the DTD, and even that is pointless as the purpose of entities is to make it easier for people to type the document. Computers don't need that help.

    However if you have a use case where having a computer-generated internal DTD would provide some value, I would like to see it.
    how can i create the new XML file and give it its name?
    Check out the documentation for StreamResult. You'll see it has constructors that take a File or an OutputStream.