xml-general mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Mark Norman <mnor...@cup.hp.com>
Subject creating DOCTYPE directive
Date Tue, 23 Jan 2001 19:22:56 GMT
Hello,

I am using the w3c DOM API to create an XML document.
How can I add the DOCTYPE directive to the XML? I
want this to appear in my XML:

<!DOCTYPE locations SYSTEM="locations.dtd">



Here is the code I have working so far:

 // Create the XML file which holds the location/job data
 private void createXML(ArrayList locations)
 {

  try {

   Document doc = new DocumentImpl();
   File dir = new File(DATA_DIR);
   File file = File.createTempFile(XML_FNAME, ".xml", dir);
   xmlPathName = DATA_DIR + "/" + file.getName();
   file.deleteOnExit();
   PrintWriter pw =
   new PrintWriter(new BufferedWriter(new FileWriter(file)));

   Node rootNode = doc.createElement("locations");
   doc.appendChild(rootNode);

   DecimalFormat monetaryFormat = new DecimalFormat("$###,###");
   int ix;

   for(ix=0; ix<locations.size(); ix++) {

    Location location = (Location) locations.get(ix);

    Node textNode = null;

    Node locationNode = doc.createElement("location");
    rootNode.appendChild(locationNode);

    Node nameNode = doc.createElement("name");
    locationNode.appendChild(nameNode);
    textNode = doc.createTextNode(location.name);
    nameNode.appendChild(textNode);

    Node photoNode = doc.createElement("photoURL");
    locationNode.appendChild(photoNode);
    ((Element)photoNode).setAttribute("src", location.photoURL);

    Node jobtitleNode = doc.createElement("jobtitle");
    locationNode.appendChild(jobtitleNode);
    textNode = doc.createTextNode(location.jobtitle);
    jobtitleNode.appendChild(textNode);

    Node jobNode = doc.createElement("job");
    locationNode.appendChild(jobNode);
    textNode = doc.createTextNode(location.job);
    jobNode.appendChild(textNode);

    Node descriptionNode = doc.createElement("description");
    locationNode.appendChild(descriptionNode);
    textNode = doc.createTextNode(location.description);
    descriptionNode.appendChild(textNode);

    String salary = monetaryFormat.format(location.salary);
    Node salaryNode = doc.createElement("salary");
    locationNode.appendChild(salaryNode);
    textNode = doc.createTextNode(salary);
    salaryNode.appendChild(textNode);

    String allowance = monetaryFormat.format(location.allowance);
    Node allowanceNode = doc.createElement("allowance");
    locationNode.appendChild(allowanceNode);
    textNode = doc.createTextNode(allowance);
    allowanceNode.appendChild(textNode);
   }

   OutputFormat of = new OutputFormat("XML", "UTF-8", true);
   XMLSerializer xmlser = new XMLSerializer (pw, of);
   xmlser.serialize(doc);
   pw.close();
  }

  catch(Exception exception) {
   System.err.print("XML creation error: ");
   System.err.println(exception);
  }
 }

thanks,

Mark Norman


Mime
View raw message