/*
 * 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.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
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 XmlWrite1 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        XmlWrite1 xwrite = new XmlWrite1();
        xwrite.xmlwrite();
    }
    
    public void xmlwrite() {
        try {
            File newXML = new File("C:\\zz_work\\java\\test.xml");            
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            DOMImplementation domImpl = builder.getDOMImplementation();
            
            Document document = domImpl.createDocument("","searchdata",null);
            Element root = document.getDocumentElement();
            
            //入力ファイル
            File infile = new File("C:\\zz_work\\java\\Yahoo!ファイナンス.txt");
            FileInputStream fileinputstream = new FileInputStream(infile);
            InputStreamReader inputstreamreader = new InputStreamReader(fileinputstream, "UTF-8");
            BufferedReader bufferedreader = new BufferedReader(inputstreamreader);
            
            String UrlAdress = bufferedreader.readLine();
            
            Element url = document.createElement("url");
            url.appendChild(document.createTextNode(UrlAdress));
            root.appendChild(url);
            
            
            String rec;
            while ((rec = bufferedreader.readLine()) != null) {
                String[] recary = rec.split("\t", -1);
                
                Element slist     = document.createElement("searchlist");
                Element item      = document.createElement("item");
                Element htmltag   = document.createElement("htmltag");
                Element htmlid    = document.createElement("htmlid");
                Element htmlclass = document.createElement("htmlclass");
                Element around    = document.createElement("around");
                Element regexp    = document.createElement("regexp");
                
                item.appendChild(document.createTextNode(recary[0]));
                htmltag.appendChild(document.createTextNode(recary[1]));
                htmlid.appendChild(document.createTextNode(recary[2]));
                htmlclass.appendChild(document.createTextNode(recary[3]));
                around.appendChild(document.createTextNode(recary[4]));
                regexp.appendChild(document.createTextNode(recary[5]));
                
                slist.appendChild(item);
                slist.appendChild(htmltag);
                slist.appendChild(htmlid);
                slist.appendChild(htmlclass);
                slist.appendChild(around);
                slist.appendChild(regexp);

                root.appendChild(slist);
                
            }
            
            //出力
            TransformerFactory transFactory = TransformerFactory.newInstance();
            Transformer transformer = transFactory.newTransformer();
            
            DOMSource source = new DOMSource(document);
            FileOutputStream os = new FileOutputStream(newXML);
            StreamResult result = new StreamResult(os);
            transformer.transform(source, result);
            
            
        } catch (ParserConfigurationException ex) {
            Logger.getLogger(XmlWrite1.class.getName()).log(Level.SEVERE, null, ex);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(XmlWrite1.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedEncodingException ex) {
            Logger.getLogger(XmlWrite1.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(XmlWrite1.class.getName()).log(Level.SEVERE, null, ex);
        } catch (TransformerConfigurationException ex) {
            Logger.getLogger(XmlWrite1.class.getName()).log(Level.SEVERE, null, ex);
        } catch (TransformerException ex) {
            Logger.getLogger(XmlWrite1.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        
        
    }
    
}
