解答例 - 実習課題3 - 7.DOMと名前空間
(実習課題3)
以下のXMLを出力するプログラムを作成しなさい。
<?xml version="1.0" encoding="UTF-8"?> <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"> <soapenv:Body> <m:customer xmlns:m="http://www.techscore.com/customerMessage/"> <m:name>山田太郎</m:name> <m:age>31</m:age> </m:customer> </soapenv:Body> </soapenv:Envelope>
- プログラムの実行時に、顧客名と年齢を指定可能にする。
- 1番目の引数に顧客名を指定する。指定した顧客名は、name要素の内容となる。
- 2番目の引数に年齢を指定する。指定した年齢はage要素の内容となる。
解答例
package com.techscore.dom.chapter7.exercise3; 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; /** * NamespaceXmlCreator.java * * TECHSCORE Java XML DOM XSLT 7章 実習課題3 * * Copyright (c) 2004 Four-Dimensional Data, Inc. */ public class NamespaceXmlCreator { public static void main(String[] args) throws Exception { if (args.length < 2) { System.out.println("Usage: java NamespaceXmlCreator NAME AGE"); return; } String envelope_namespace_uri = "http://schemas.xmlsoap.org/soap/envelope/"; String customer_namespace_uri = "http://www.techscore.com/customerMessage/"; 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(envelope_namespace_uri, "Envelope", null); Element envelope = document.getDocumentElement(); Attr envelopeDeclare = document.createAttributeNS(xmlns_namespace_uri, "soapenv"); envelopeDeclare.setPrefix("xmlns"); envelopeDeclare.setValue(envelope_namespace_uri); envelope.setAttributeNodeNS(envelopeDeclare); envelope.setPrefix("soapenv"); Element body = document.createElementNS(xmlns_namespace_uri, "Body"); body.setPrefix("soapenv"); envelope.appendChild(body); Element customer = document.createElementNS(customer_namespace_uri, "customer"); customer.setPrefix("m"); Attr customerTagDeclare = document.createAttributeNS(xmlns_namespace_uri, "m"); customerTagDeclare.setPrefix("xmlns"); customerTagDeclare.setValue(customer_namespace_uri); customer.setAttributeNodeNS(customerTagDeclare); body.appendChild(customer); Element name = document.createElementNS(customer_namespace_uri, "name"); name.setPrefix("m"); name.appendChild(document.createTextNode(args[0])); customer.appendChild(name); Element age = document.createElementNS(customer_namespace_uri, "age"); age.setPrefix("m"); age.appendChild(document.createTextNode(args[1])); customer.appendChild(age); 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); } }