/*
 * 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 XmlRead2 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        XmlRead2 a = new XmlRead2();
        a.xmlread();
    }
    
    public void xmlread() {
        try {
            File inXML = new File("C:\\zz_work\\java\\test.xml");
            // XML ドキュメントの読み込み
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document doc = builder.parse( inXML );
            
            // ルート要素の取得
            Element root = doc.getDocumentElement();
            
            // URL
            NodeList url = root.getElementsByTagName("url");
            Node urlnode = url.item(0);
            System.out.println(urlnode.getFirstChild().getNodeValue());
                        
            // 検索情報
            NodeList slist = root.getElementsByTagName("searchlist");
            for(int i = 0; i < slist.getLength(); i++) {
                Node slistnode = slist.item(i);
                Node child;
                for (child = slistnode.getFirstChild(); child != null; child = child.getNextSibling()) {
                    if(child.getNodeType() == Node.ELEMENT_NODE) {
                        String tag = child.getNodeName();
                        
                        String rtn = "";
                        if(child.getFirstChild() != null) {
                            rtn = child.getFirstChild().getNodeValue();
                        }
                        
                        switch (tag) {
                            case "item":
                                System.out.println("item = " + rtn);
                                break;
                            case "htmltag":
                                System.out.println("htmltag = " + rtn);
                                break;
                            case "htmlid":
                                System.out.println("htmlid = " + rtn);
                                break;
                            case "htmlclass":
                                System.out.println("htmlclass = " + rtn);
                                break;
                            case "around":
                                System.out.println("around = " + rtn);
                                break;
                            case "regexp":
                                System.out.println("regexp = " + rtn);
                                break;
                        }
                        
                    }
                    
                }
                
            }
            
        } catch (ParserConfigurationException | SAXException | IOException ex) {
            Logger.getLogger(XmlRead2.class.getName()).log(Level.SEVERE, null, ex);
        }
        
    }
    
}
