/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package XmlTest1;

import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

/**
 *
 * @author kgto
 */
public class XmlRead1 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        XmlRead1 a = new XmlRead1();
        a.getnode();
    }
    
    public void getnode() {
        try {
            // XML ドキュメントの読み込み
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            File f = new File( "MyTest.gwt.xml" );
            Document doc = builder.parse( f );
            
            // ルート要素の取得
            Element root = doc.getDocumentElement();
            // 子要素リストの取得
            NodeList children = root.getChildNodes();
            
            nodePrint(doc);

            /*
            // 子要素のタグ名を出力
            for(int i = 0; i < children.getLength(); i++) {
                Node child = children.item(i);
                if( child instanceof Element ) {
                    Element childElement = (Element) child;
                    System.out.println( childElement.getTagName() + "  " 
                            + childElement.getNodeName() + "  "
                            + childElement.getNodeValue()
                    );
                    
                }
            }
            */
            
        } catch (ParserConfigurationException | IOException | SAXException ex) {
            Logger.getLogger(XmlRead1.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public void nodePrint(Node element) {
        
        NodeList nodes = element.getChildNodes();
        for (int i = 0; i < nodes.getLength(); i++) {
            Node node2 = nodes.item(i);
            if(node2 instanceof Element) {
                Element childElement = (Element)node2;
                //System.out.println(childElement.hasChildNodes());
                System.out.println(childElement.getNodeName() + " = " +
                                   childElement.getNodeValue() + "  " +
                                   childElement.getNodeType()
                );
                nodePrint(childElement);
            }
            
        }
        
    }

}
