[XML-DEV Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: MSXML : Create a document with a doctype declaration ?

  • From: rbourret@d... (Ron Bourret)
  • To: xml-dev@i...
  • Date: Tue, 28 Apr 1998 15:44:27 +0200

msxml create document
> How to create a new XML document with a doctype declaration with msxml ?
> 
> Something like :
> 
> <!DOCTYPE foo SYSTEM "http://foo.domain.xx">
> <foo>
> ...
> </foo>

Try the following, which creates a new Document object, adds elements, and saves it.  
You might need to change the document encoding for it to work on your machine.  The 
output file is:

<?xml version="1.0">
<!DOCTYPE foo SYSTEM "http://foo.domain.xx">
<foo>
     <bar>foobar</bar>
</foo>


import com.ms.xml.om.Document;
import com.ms.xml.om.Element;
import com.ms.xml.util.XMLOutputStream;
import com.ms.xml.util.Name;

import java.io.FileOutputStream;

public class CreateFooBar
{
   static public void main(String argv[])
   {
      try
      {
         Document d = createMSXMLDoc();
         saveDoc(d);
      }
      catch (Exception e)
      {
      }
   }
   
   static public Document createMSXMLDoc()
   {
      Document d = new Document();
      Element  xmlPINode, dtdNode, root, child, pcdata;

      // Create the XML PI node.
      xmlPINode = d.createElement(Element.PI, "xml");
      d.addChild(xmlPINode, null);
      d.setVersion("1.0");
      
      // Create the DOCTYPE node.
      dtdNode = d.createElement(Element.DTD);
      d.addChild(dtdNode, xmlPINode);
      dtdNode.setAttribute(Name.create("NAME"), Name.create("foo"));
      dtdNode.setAttribute(Name.create("URL"), Name.create("http://foo.domain.xx"));
      
      // Create the root element
      root = d.createElement(Element.ELEMENT, "foo");
      d.addChild(root, dtdNode);
      
      // Create a child element
      child = d.createElement(root, Element.ELEMENT, Name.create("bar"), null);
      
      // Create PCDATA for the child element
      pcdata = d.createElement(child, Element.PCDATA, null, "foobar");
      
      return (d);
   }

   static public void saveDoc(Document d) throws Exception
   {
      d.setEncoding("ASCII");
      d.setOutputStyle(XMLOutputStream.PRETTY);
      FileOutputStream file = new FileOutputStream("foobar.xml");
      XMLOutputStream xmlFile = d.createOutputStream(file);
      d.save(xmlFile);
   }
}


xml-dev: A list for W3C XML Developers. To post, mailto:xml-dev@i...
Archived as: http://www.lists.ic.ac.uk/hypermail/xml-dev/
To (un)subscribe, mailto:majordomo@i... the following message;
(un)subscribe xml-dev
To subscribe to the digests, mailto:majordomo@i... the following message;
subscribe xml-dev-digest
List coordinator, Henry Rzepa (mailto:rzepa@i...)


PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
 

Stylus Studio has published XML-DEV in RSS and ATOM formats, enabling users to easily subcribe to the list from their preferred news reader application.


Stylus Studio Sponsored Links are added links designed to provide related and additional information to the visitors of this website. they were not included by the author in the initial post. To view the content without the Sponsor Links please click here.

Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.