目次へ

解答例 - 実習課題2 - 7.DOMと名前空間

(実習課題2)

7.4で例に挙げたプログラムを完成させなさい。

解答例

package com.techscore.dom.chapter7.exercise2;

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.Attr;
import org.w3c.dom.DOMImplementation;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

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

public class WriteJSPFile {

    public static void main(String[] args) throws Exception{
        String xhtml_namespace_uri="http://www.w3.org/1999/xhtml";
        String htmltag_namespace_uri="http://jakarta.apache.org/struts/tags-html-1.0";
        String xmlns_namespace_uri="http://www.w3.org/2000/xmlns/";
        
        DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(true);
        DocumentBuilder builder=factory.newDocumentBuilder();
        DOMImplementation domImpl=builder.getDOMImplementation();
        Document document=domImpl.createDocument(xhtml_namespace_uri,"html",null);
        
        Element html=document.getDocumentElement();
        
        Attr xhtmlDeclare=document.createAttributeNS(xmlns_namespace_uri,"xmlns");
        xhtmlDeclare.setValue(xhtml_namespace_uri);
        html.setAttributeNodeNS(xhtmlDeclare);
        
        Element body = document.createElementNS(xhtml_namespace_uri, "body");
        html.appendChild(body);

        Element form=document.createElementNS(xhtml_namespace_uri,"form");
        body.appendChild(form);
        
        Attr action=document.createAttributeNS(null,"action");
        action.setValue("/sample2/productInput.jsp");
        form.setAttributeNodeNS(action);
        Attr method = document.createAttributeNS(null, "method");
        method.setValue("post");
        form.setAttributeNodeNS(method);
        
        Element table = document.createElementNS(xhtml_namespace_uri, "table");
        Element tr = document.createElementNS(xhtml_namespace_uri, "tr");
        Element td1 = document.createElementNS(xhtml_namespace_uri, "td");
        td1.appendChild(document.createTextNode("商品名"));
        tr.appendChild(td1);
        
        Element input = document.createElementNS(xhtml_namespace_uri, "input");
        Attr type = document.createAttributeNS(null, "type");
        type.setValue("text");
        input.setAttributeNodeNS(type);
        Attr name = document.createAttributeNS(null, "name");
        name.setValue("name");
        input.setAttributeNodeNS(name);
        
        Element td2 = document.createElementNS(xhtml_namespace_uri, "td");
        td2.appendChild(input);
        tr.appendChild(td2);

        table.appendChild(tr);
        form.appendChild(table);
        
        
        Element formWithPrefix=document.createElementNS(htmltag_namespace_uri,"form");
        Attr action2=document.createAttributeNS(null,"action");
        action2.setValue("/sample/productInput.do");
        formWithPrefix.setAttributeNodeNS(action2);
        Attr method2 = document.createAttributeNS(null, "method");
        method2.setValue("post");
        formWithPrefix.setAttributeNodeNS(method2);
        body.appendChild(formWithPrefix);
        
        Attr htmlTagDeclare=document.createAttributeNS(xmlns_namespace_uri,"html");
        htmlTagDeclare.setPrefix("xmlns");
        htmlTagDeclare.setValue(htmltag_namespace_uri);
        formWithPrefix.setAttributeNodeNS(htmlTagDeclare);
        
        formWithPrefix.setPrefix("html");
        
        Element table2 = document.createElementNS(xhtml_namespace_uri, "table");
        Element tr2 = document.createElementNS(xhtml_namespace_uri, "tr");
        Element td3 = document.createElementNS(xhtml_namespace_uri, "td");
        td3.appendChild(document.createTextNode("商品名"));
        tr2.appendChild(td3);
        
        Element td4 = document.createElementNS(xhtml_namespace_uri, "td");
        Element inputWithPrefix = document.createElementNS(xmlns_namespace_uri, "input");
        Attr typeInput = document.createAttributeNS(xmlns_namespace_uri, "type");
        typeInput.setValue("text");
        Attr propertyInput = document.createAttributeNS(xmlns_namespace_uri, "property");
        propertyInput.setValue("property");
        inputWithPrefix.setAttributeNodeNS(typeInput);
        inputWithPrefix.setAttributeNodeNS(propertyInput);              
        inputWithPrefix.setPrefix("html");
        
        td4.appendChild(inputWithPrefix);
        
        tr2.appendChild(td4);           
        table2.appendChild(tr2);
        formWithPrefix.appendChild(table2);
        
        
        TransformerFactory transFactory = TransformerFactory.newInstance();
        Transformer transformer = transFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT,"yes");
        transformer.setOutputProperty(OutputKeys.ENCODING,"EUC-JP");
        DOMSource source = new DOMSource(document);
        StreamResult result = new StreamResult(System.out);
        transformer.transform(source, result);
        }
}

↑このページの先頭へ

こちらもチェック!

PR
  • XMLDB.jp