【Java】【XML】DOM [4]

DOM サンプル [4]

 DOM を使ったサンプルプログラム(XML作成およびコンソールへの出力)

サンプル

printDomTree.java

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;
import org.w3c.dom.*;

public class printDomTree {
	public static void main(String args[]) {
		BufferedReader br = null;
		
		try {
			/* To get a String of Root element */
			br = new BufferedReader(
					new InputStreamReader(System.in));
			System.out.println("Input root element : " );
			String root = br.readLine();
			
			DocumentBuilderFactory factory =
				DocumentBuilderFactory.newInstance();
			DocumentBuilder builder =
				factory.newDocumentBuilder();
			Document doc = builder.newDocument();
			
			Element rootElement = doc.createElement(root);
			doc.appendChild(rootElement);
			
			/* To create a comment */
			System.out.println("Input a comment : " );
			String comStr = br.readLine();
			Comment comment = doc.createComment(comStr);
			rootElement.appendChild(comment);
			
			for (int i = 1; i <= 3; i++) {
				/* To get a string of a child element */
				System.out.println("Input the element : " );
				String element = br.readLine();

				/* To get a string of a child element value */
				System.out.println("Input the data : " );
				String data = br.readLine();
				
				Element childElm = doc.createElement(element);
				childElm.appendChild(doc.createTextNode(data));
				rootElement.appendChild(childElm);
				
				if (i == 3) {
					System.out.println(
							"Input the attribute to the child" );
					String name = br.readLine();
					
					System.out.println(
							"Input the value of the attribute to the child" );
					String value = br.readLine();
					
					childElm.setAttribute(name, value);
				}
			}
			
			System.out.println();
			System.out.println("[Output]" );
			
			/* To print a DOM tree on a console */
			TransformerFactory tranFatory = TransformerFactory.newInstance();
			Transformer transFormer = tranFatory.newTransformer();
			Source source = new DOMSource(doc);
			Result result = new StreamResult(System.out);
			transFormer.transform(source, result);
		} catch (Exception ex) {
			System.out.println(ex);
			System.out.println("Error..." );
		} finally {
			if (br != null)
				try {
					br.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
		}
	}
}

出力

Input root element : 
rootData
Input a comment : 
This is a comment
Input the element : 
childElm1
Input the data : 
Data1
Input the element : 
childElm2
Input the data : 
Data2!
Input the element : 
childElm3
Input the data : 
Data3?
Input the attribute to the child
attrToChildElm
Input the value of the attribute to the child
valueOfAttrToChildElm

[Output]
<?xml version="1.0" encoding="UTF-8" standalone="no"?><rootData><!--This is a comment--><childElm1>Data1</childElm1><childElm2>Data2!</childElm2><childElm3 attrToChildElm="valueOfAttrToChildElm">Data3?</childElm3></rootData>