目次へ

解答例 - 実習課題2 - 6.DTD(DocumentType)

(実習課題2)

以下のXML文書を出力するプログラムを作成しなさい

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE person PUBLIC "-//Four Dimensional Data, Inc.//DTD DOM Example 1.0//EN"
        "DOMExample.dtd">
<person>
<name>Taro Yamada</name>
<age>31</age>
<license>Sun Certified Programmer</license>
<license>Sun Certified Developer</license>
</person>

解答例

package com.techscore.dom.chapter6.exercise2;

import java.io.File;
import java.io.FileOutputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.DOMImplementation;
import org.w3c.dom.Document;
import org.w3c.dom.DocumentType;
import org.w3c.dom.Element;

/**
 * DtdXmlFileCreator.java
 * 
 * TECHSCORE Java XML DOM XSLT 6章 実習課題2
 * 
 * Copyright (c) 2004 Four-Dimensional Data, Inc.
 */

public class DtdXmlFileCreator {

    public static String PUBLIC_ID = "-//Four Dimensional Data, Inc.//DTD DOM Example 1.0//EN";
    public static String SYSTEM_ID = "DOMExample.dtd";

    public static void main(String[] args) throws Exception {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();

        DOMImplementation domImpl = builder.getDOMImplementation();

        DocumentType docType = domImpl.createDocumentType("person",
                                                          PUBLIC_ID,
                                                          SYSTEM_ID);

        // XMLの生成
        Document document = domImpl.createDocument("", "person", docType);
        Element person = document.getDocumentElement();

        person.appendChild(document.createTextNode("\n"));

        Element name = document.createElement("name");
        name.appendChild(document.createTextNode("Taro Yamada"));
        person.appendChild(name);

        person.appendChild(document.createTextNode("\n"));

        Element age = document.createElement("age");
        age.appendChild(document.createTextNode("31"));
        person.appendChild(age);

        person.appendChild(document.createTextNode("\n"));

        Element license = document.createElement("license");
        license.appendChild(document.createTextNode("Sun Certified Programmer"));
        person.appendChild(license);

        person.appendChild(document.createTextNode("\n"));

        license = document.createElement("license");
        license.appendChild(document.createTextNode("Sun Certified Developer"));
        person.appendChild(license);

        person.appendChild(document.createTextNode("\n"));

        // DTD部分の表示
        TransformerFactory transFactory = TransformerFactory.newInstance();
        Transformer transformer = transFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty(OutputKeys.METHOD, "xml");
        transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, SYSTEM_ID);
        transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, PUBLIC_ID);

        // XMLファイルを出力
        DOMSource source = new DOMSource(document);
        File buildFile = new File("personByChap6_2.xml");
        FileOutputStream os = new FileOutputStream(buildFile);
        StreamResult result = new StreamResult(os);

        transformer.transform(source, result);
    }
}

↑このページの先頭へ

こちらもチェック!

PR
  • XMLDB.jp