/*
 * 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 Form.SearchDataRW;
import WebScraping.SearchData;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.ArrayList;
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 javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.DOMImplementation;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

/**
 *
 * @author kgto
 */
public class XmlSearchDataRW1 {
    
    SearchDataRW sdatRW = new SearchDataRW();
    
    String urladdress;
    ArrayList slist;
    
    Document document;

    XmlSearchDataRW1() {
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        XmlSearchDataRW1 RWtest1 = new XmlSearchDataRW1();
        
        RWtest1.readXML();
        
        RWtest1.writeXML();
    }
    
    void readXML() {
        File file = new File("C:\\zz_work\\java\\NetBeansProjects\\WebScraping\\data\\Yahoo!天気.xml");
        sdatRW.load(file);
        // 読込み項目
        urladdress = sdatRW.geturl();
        slist = sdatRW.getslist();
        
    }
    
    public void writeXML() {
        try {
            File file = new File("test.xml");
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            DOMImplementation domImpl = builder.getDOMImplementation();
            
            document = domImpl.createDocument("","searchdata",null);
            Element root = document.getDocumentElement();
            
            // ＵＲＬ
            Element url = document.createElement("url");
            url.appendChild(document.createTextNode(urladdress));
            root.appendChild(url);
            
            // 検索情報
            for (Object slist1 : slist) {
                SearchData sdat = (SearchData) slist1;
                
                Element cslist    = document.createElement("searchlist");
                
                addChild(cslist, "item", sdat.getitem());
                addChild(cslist, "htmltag", sdat.getHtmltag());
                addChild(cslist, "htmlid", sdat.getHtmlid());
                addChild(cslist, "htmlclass", sdat.getHtmlclass());
                addChild(cslist, "around", sdat.getaround());
                addChild(cslist, "regexp", sdat.getregexp());
                
                root.appendChild(cslist);
            }
            
            // 出力
            TransformerFactory transFactory = TransformerFactory.newInstance();
            Transformer transformer = transFactory.newTransformer();
            
            DOMSource source = new DOMSource(document);
            FileOutputStream os = new FileOutputStream(file);
            StreamResult result = new StreamResult(os);
            transformer.transform(source, result);
            
        } catch (ParserConfigurationException | FileNotFoundException ex) {
            Logger.getLogger(SearchDataRW.class.getName()).log(Level.SEVERE, null, ex);
        } catch (TransformerConfigurationException ex) {
            Logger.getLogger(SearchDataRW.class.getName()).log(Level.SEVERE, null, ex);
        } catch (TransformerException ex) {
            Logger.getLogger(SearchDataRW.class.getName()).log(Level.SEVERE, null, ex);
        }
        
    }
    
    private void addChild(Element cslist, String keyword, String data) {
        if(!data.isEmpty()) {
            Element element = document.createElement(keyword);
            element.appendChild(document.createTextNode(data));
            cslist.appendChild(element);
        }
    }
    
}
