作業部屋の使い方を試しています。
(empty log message)
| @@ -0,0 +1 @@ | ||
| 1 | +<?xml version="1.0" encoding="UTF-8" standalone="no"?><searchdata><url>http://weather.yahoo.co.jp/weather/</url><searchlist><item>天気01</item><htmltag>li</htmltag><htmlclass>point pt1400</htmlclass></searchlist><searchlist><item>天気02</item><htmltag>li</htmltag><htmlclass>point pt1900</htmlclass></searchlist><searchlist><item>天気03</item><htmltag>li</htmltag><htmlclass>point pt3410</htmlclass></searchlist><searchlist><item>天気04</item><htmltag>li</htmltag><htmlclass>point pt4410</htmlclass></searchlist><searchlist><item>天気05</item><htmltag>li</htmltag><htmlclass>point pt5110</htmlclass></searchlist><searchlist><item>天気06</item><htmltag>li</htmltag><htmlclass>point pt5410</htmlclass></searchlist><searchlist><item>天気07</item><htmltag>li</htmltag><htmlclass>point pt5610</htmlclass></searchlist><searchlist><item>天気08</item><htmltag>li</htmltag><htmlclass>point pt6200</htmlclass></searchlist><searchlist><item>天気09</item><htmltag>li</htmltag><htmlclass>point pt6710</htmlclass></searchlist><searchlist><item>天気10</item><htmltag>li</htmltag><htmlclass>point pt7410</htmlclass></searchlist><searchlist><item>天気11</item><htmltag>li</htmltag><htmlclass>point pt8210</htmlclass></searchlist><searchlist><item>天気12</item><htmltag>li</htmltag><htmlclass>point pt8810</htmlclass></searchlist><searchlist><item>天気13</item><htmltag>li</htmltag><htmlclass>point pt9110</htmlclass></searchlist></searchdata> | |
| \ No newline at end of file |
| @@ -896,6 +896,14 @@ | ||
| 896 | 896 | <propertyfile file="${built-jar.properties}"> |
| 897 | 897 | <entry key="${basedir}" value=""/> |
| 898 | 898 | </propertyfile> |
| 899 | + <antcall target="-maybe-call-dep"> | |
| 900 | + <param name="call.built.properties" value="${built-jar.properties}"/> | |
| 901 | + <param location="${project.WebScraping}" name="call.subproject"/> | |
| 902 | + <param location="${project.WebScraping}/build.xml" name="call.script"/> | |
| 903 | + <param name="call.target" value="jar"/> | |
| 904 | + <param name="transfer.built-jar.properties" value="${built-jar.properties}"/> | |
| 905 | + <param name="transfer.not.archive.disabled" value="true"/> | |
| 906 | + </antcall> | |
| 899 | 907 | </target> |
| 900 | 908 | <target depends="init,-check-automatic-build,-clean-after-automatic-build" name="-verify-automatic-build"/> |
| 901 | 909 | <target depends="init" name="-check-automatic-build"> |
| @@ -1381,6 +1389,14 @@ | ||
| 1381 | 1389 | <propertyfile file="${built-clean.properties}"> |
| 1382 | 1390 | <entry key="${basedir}" value=""/> |
| 1383 | 1391 | </propertyfile> |
| 1392 | + <antcall target="-maybe-call-dep"> | |
| 1393 | + <param name="call.built.properties" value="${built-clean.properties}"/> | |
| 1394 | + <param location="${project.WebScraping}" name="call.subproject"/> | |
| 1395 | + <param location="${project.WebScraping}/build.xml" name="call.script"/> | |
| 1396 | + <param name="call.target" value="clean"/> | |
| 1397 | + <param name="transfer.built-clean.properties" value="${built-clean.properties}"/> | |
| 1398 | + <param name="transfer.not.archive.disabled" value="true"/> | |
| 1399 | + </antcall> | |
| 1384 | 1400 | </target> |
| 1385 | 1401 | <target depends="init" name="-do-clean"> |
| 1386 | 1402 | <delete dir="${build.dir}"/> |
| @@ -11,5 +11,15 @@ | ||
| 11 | 11 | <root id="test.src.dir"/> |
| 12 | 12 | </test-roots> |
| 13 | 13 | </data> |
| 14 | + <references xmlns="http://www.netbeans.org/ns/ant-project-references/1"> | |
| 15 | + <reference> | |
| 16 | + <foreign-project>WebScraping</foreign-project> | |
| 17 | + <artifact-type>jar</artifact-type> | |
| 18 | + <script>build.xml</script> | |
| 19 | + <target>jar</target> | |
| 20 | + <clean-target>clean</clean-target> | |
| 21 | + <id>jar</id> | |
| 22 | + </reference> | |
| 23 | + </references> | |
| 14 | 24 | </configuration> |
| 15 | 25 | </project> |
| @@ -0,0 +1,45 @@ | ||
| 1 | +/* | |
| 2 | + * To change this license header, choose License Headers in Project Properties. | |
| 3 | + * To change this template file, choose Tools | Templates | |
| 4 | + * and open the template in the editor. | |
| 5 | + */ | |
| 6 | +package XmlTest1; | |
| 7 | + | |
| 8 | +import java.io.File; | |
| 9 | +import java.util.ArrayList; | |
| 10 | + | |
| 11 | +/** | |
| 12 | + * | |
| 13 | + * @author kgto | |
| 14 | + */ | |
| 15 | +public class XmlSearchData2main { | |
| 16 | + | |
| 17 | + XmlSearchDataRW2 xmlrw = new XmlSearchDataRW2(); | |
| 18 | + | |
| 19 | + /** | |
| 20 | + * @param args the command line arguments | |
| 21 | + */ | |
| 22 | + public static void main(String[] args) { | |
| 23 | + | |
| 24 | + XmlSearchData2main testmain = new XmlSearchData2main(); | |
| 25 | + | |
| 26 | + File file = new File("test.xml"); | |
| 27 | + | |
| 28 | + testmain.testsave(file); | |
| 29 | + | |
| 30 | + } | |
| 31 | + | |
| 32 | + public XmlSearchData2main() { | |
| 33 | + } | |
| 34 | + | |
| 35 | + public void testsave(File file) { | |
| 36 | + | |
| 37 | + ArrayList slist = new ArrayList(); | |
| 38 | + | |
| 39 | + xmlrw.saveUrl(""); | |
| 40 | + xmlrw.saveSearchList(slist); | |
| 41 | + xmlrw.write(file); | |
| 42 | + | |
| 43 | + } | |
| 44 | + | |
| 45 | +} |
| @@ -0,0 +1,123 @@ | ||
| 1 | +/* | |
| 2 | + * To change this license header, choose License Headers in Project Properties. | |
| 3 | + * To change this template file, choose Tools | Templates | |
| 4 | + * and open the template in the editor. | |
| 5 | + */ | |
| 6 | +package XmlTest1; | |
| 7 | + | |
| 8 | +import Form.SearchDataRW; | |
| 9 | +import WebScraping.SearchData; | |
| 10 | +import java.io.File; | |
| 11 | +import java.io.FileNotFoundException; | |
| 12 | +import java.io.FileOutputStream; | |
| 13 | +import java.util.ArrayList; | |
| 14 | +import java.util.logging.Level; | |
| 15 | +import java.util.logging.Logger; | |
| 16 | +import javax.xml.parsers.DocumentBuilder; | |
| 17 | +import javax.xml.parsers.DocumentBuilderFactory; | |
| 18 | +import javax.xml.parsers.ParserConfigurationException; | |
| 19 | +import javax.xml.transform.Transformer; | |
| 20 | +import javax.xml.transform.TransformerConfigurationException; | |
| 21 | +import javax.xml.transform.TransformerException; | |
| 22 | +import javax.xml.transform.TransformerFactory; | |
| 23 | +import javax.xml.transform.dom.DOMSource; | |
| 24 | +import javax.xml.transform.stream.StreamResult; | |
| 25 | +import org.w3c.dom.DOMImplementation; | |
| 26 | +import org.w3c.dom.Document; | |
| 27 | +import org.w3c.dom.Element; | |
| 28 | + | |
| 29 | +/** | |
| 30 | + * | |
| 31 | + * @author kgto | |
| 32 | + */ | |
| 33 | +public class XmlSearchDataRW1 { | |
| 34 | + | |
| 35 | + SearchDataRW sdatRW = new SearchDataRW(); | |
| 36 | + | |
| 37 | + String urladdress; | |
| 38 | + ArrayList slist; | |
| 39 | + | |
| 40 | + Document document; | |
| 41 | + | |
| 42 | + XmlSearchDataRW1() { | |
| 43 | + } | |
| 44 | + | |
| 45 | + /** | |
| 46 | + * @param args the command line arguments | |
| 47 | + */ | |
| 48 | + public static void main(String[] args) { | |
| 49 | + XmlSearchDataRW1 RWtest1 = new XmlSearchDataRW1(); | |
| 50 | + | |
| 51 | + RWtest1.readXML(); | |
| 52 | + | |
| 53 | + RWtest1.writeXML(); | |
| 54 | + } | |
| 55 | + | |
| 56 | + void readXML() { | |
| 57 | + File file = new File("C:\\zz_work\\java\\NetBeansProjects\\WebScraping\\data\\Yahoo!天気.xml"); | |
| 58 | + sdatRW.load(file); | |
| 59 | + // 読込み項目 | |
| 60 | + urladdress = sdatRW.geturl(); | |
| 61 | + slist = sdatRW.getslist(); | |
| 62 | + | |
| 63 | + } | |
| 64 | + | |
| 65 | + public void writeXML() { | |
| 66 | + try { | |
| 67 | + File file = new File("test.xml"); | |
| 68 | + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); | |
| 69 | + DocumentBuilder builder = factory.newDocumentBuilder(); | |
| 70 | + DOMImplementation domImpl = builder.getDOMImplementation(); | |
| 71 | + | |
| 72 | + document = domImpl.createDocument("","searchdata",null); | |
| 73 | + Element root = document.getDocumentElement(); | |
| 74 | + | |
| 75 | + // URL | |
| 76 | + Element url = document.createElement("url"); | |
| 77 | + url.appendChild(document.createTextNode(urladdress)); | |
| 78 | + root.appendChild(url); | |
| 79 | + | |
| 80 | + // 検索情報 | |
| 81 | + for (Object slist1 : slist) { | |
| 82 | + SearchData sdat = (SearchData) slist1; | |
| 83 | + | |
| 84 | + Element cslist = document.createElement("searchlist"); | |
| 85 | + | |
| 86 | + addChild(cslist, "item", sdat.getitem()); | |
| 87 | + addChild(cslist, "htmltag", sdat.getHtmltag()); | |
| 88 | + addChild(cslist, "htmlid", sdat.getHtmlid()); | |
| 89 | + addChild(cslist, "htmlclass", sdat.getHtmlclass()); | |
| 90 | + addChild(cslist, "around", sdat.getaround()); | |
| 91 | + addChild(cslist, "regexp", sdat.getregexp()); | |
| 92 | + | |
| 93 | + root.appendChild(cslist); | |
| 94 | + } | |
| 95 | + | |
| 96 | + // 出力 | |
| 97 | + TransformerFactory transFactory = TransformerFactory.newInstance(); | |
| 98 | + Transformer transformer = transFactory.newTransformer(); | |
| 99 | + | |
| 100 | + DOMSource source = new DOMSource(document); | |
| 101 | + FileOutputStream os = new FileOutputStream(file); | |
| 102 | + StreamResult result = new StreamResult(os); | |
| 103 | + transformer.transform(source, result); | |
| 104 | + | |
| 105 | + } catch (ParserConfigurationException | FileNotFoundException ex) { | |
| 106 | + Logger.getLogger(SearchDataRW.class.getName()).log(Level.SEVERE, null, ex); | |
| 107 | + } catch (TransformerConfigurationException ex) { | |
| 108 | + Logger.getLogger(SearchDataRW.class.getName()).log(Level.SEVERE, null, ex); | |
| 109 | + } catch (TransformerException ex) { | |
| 110 | + Logger.getLogger(SearchDataRW.class.getName()).log(Level.SEVERE, null, ex); | |
| 111 | + } | |
| 112 | + | |
| 113 | + } | |
| 114 | + | |
| 115 | + private void addChild(Element cslist, String keyword, String data) { | |
| 116 | + if(!data.isEmpty()) { | |
| 117 | + Element element = document.createElement(keyword); | |
| 118 | + element.appendChild(document.createTextNode(data)); | |
| 119 | + cslist.appendChild(element); | |
| 120 | + } | |
| 121 | + } | |
| 122 | + | |
| 123 | +} |
| @@ -0,0 +1,208 @@ | ||
| 1 | +/* | |
| 2 | + * To change this license header, choose License Headers in Project Properties. | |
| 3 | + * To change this template file, choose Tools | Templates | |
| 4 | + * and open the template in the editor. | |
| 5 | + */ | |
| 6 | +package XmlTest1; | |
| 7 | + | |
| 8 | +import WebScraping.SearchData; | |
| 9 | +import java.io.File; | |
| 10 | +import java.io.FileNotFoundException; | |
| 11 | +import java.io.FileOutputStream; | |
| 12 | +import java.io.IOException; | |
| 13 | +import java.util.ArrayList; | |
| 14 | +import java.util.logging.Level; | |
| 15 | +import java.util.logging.Logger; | |
| 16 | +import javax.xml.parsers.DocumentBuilder; | |
| 17 | +import javax.xml.parsers.DocumentBuilderFactory; | |
| 18 | +import javax.xml.parsers.ParserConfigurationException; | |
| 19 | +import javax.xml.transform.Transformer; | |
| 20 | +import javax.xml.transform.TransformerConfigurationException; | |
| 21 | +import javax.xml.transform.TransformerException; | |
| 22 | +import javax.xml.transform.TransformerFactory; | |
| 23 | +import javax.xml.transform.dom.DOMSource; | |
| 24 | +import javax.xml.transform.stream.StreamResult; | |
| 25 | +import org.w3c.dom.DOMImplementation; | |
| 26 | +import org.w3c.dom.Document; | |
| 27 | +import org.w3c.dom.Element; | |
| 28 | +import org.w3c.dom.Node; | |
| 29 | +import org.w3c.dom.NodeList; | |
| 30 | +import org.xml.sax.SAXException; | |
| 31 | + | |
| 32 | +/** | |
| 33 | + * | |
| 34 | + * @author kgto | |
| 35 | + */ | |
| 36 | +public class XmlSearchDataRW2 { | |
| 37 | + | |
| 38 | + DocumentBuilder builder; | |
| 39 | + Document document; | |
| 40 | + Element root; | |
| 41 | + | |
| 42 | + public XmlSearchDataRW2() { | |
| 43 | + try { | |
| 44 | + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); | |
| 45 | + builder = factory.newDocumentBuilder(); | |
| 46 | + | |
| 47 | + } catch (ParserConfigurationException ex) { | |
| 48 | + Logger.getLogger(XmlSearchDataRW2.class.getName()).log(Level.SEVERE, null, ex); | |
| 49 | + } | |
| 50 | + } | |
| 51 | + | |
| 52 | + public String loadUrl() { | |
| 53 | + String urladdress; | |
| 54 | + | |
| 55 | + NodeList nodelist = root.getElementsByTagName("url"); | |
| 56 | + Node node = nodelist.item(0); | |
| 57 | + urladdress = node.getFirstChild().getNodeValue(); | |
| 58 | + | |
| 59 | + return urladdress; | |
| 60 | + } | |
| 61 | + | |
| 62 | + public ArrayList<SearchData> loadSearchList() { | |
| 63 | + ArrayList<SearchData> slist = new ArrayList<>(); | |
| 64 | + | |
| 65 | + NodeList nodelist = root.getElementsByTagName("searchlist"); | |
| 66 | + for(int i = 0; i < nodelist.getLength(); i++) { | |
| 67 | + Node childnode = nodelist.item(i); | |
| 68 | + | |
| 69 | + boolean sdatflg = false; | |
| 70 | + SearchData sdat = new SearchData(); | |
| 71 | + | |
| 72 | + //NodeList childnodelist = childnode.getChildNodes(); | |
| 73 | + //for(int j = 0; j < childnodelist.getLength(); j++) { | |
| 74 | + // Node child = childnodelist.item(j); | |
| 75 | + | |
| 76 | + for (Node child = childnode.getFirstChild(); | |
| 77 | + child != null; child = child.getNextSibling()) { | |
| 78 | + | |
| 79 | + if(child.getNodeType() == Node.ELEMENT_NODE) { | |
| 80 | + | |
| 81 | + String tag = child.getNodeName(); | |
| 82 | + String rtn = ""; | |
| 83 | + if(child.getFirstChild() != null) { | |
| 84 | + rtn = child.getFirstChild().getNodeValue(); | |
| 85 | + } | |
| 86 | + | |
| 87 | + switch (tag) { | |
| 88 | + case "item" : | |
| 89 | + sdat.setitem(rtn); | |
| 90 | + sdatflg = true; | |
| 91 | + break; | |
| 92 | + case "htmltag" : | |
| 93 | + sdat.setHtmltag(rtn); | |
| 94 | + sdatflg = true; | |
| 95 | + break; | |
| 96 | + case "htmlid" : | |
| 97 | + sdat.setHtmlid(rtn); | |
| 98 | + sdatflg = true; | |
| 99 | + break; | |
| 100 | + case "htmlclass" : | |
| 101 | + sdat.setHtmlclass(rtn); | |
| 102 | + sdatflg = true; | |
| 103 | + break; | |
| 104 | + case "around" : | |
| 105 | + sdat.setaround(rtn); | |
| 106 | + sdatflg = true; | |
| 107 | + break; | |
| 108 | + case "regexp" : | |
| 109 | + sdat.setregexp(rtn); | |
| 110 | + sdatflg = true; | |
| 111 | + break; | |
| 112 | + } | |
| 113 | + } | |
| 114 | + } | |
| 115 | + if(sdatflg) slist.add(sdat); | |
| 116 | + } | |
| 117 | + return slist; | |
| 118 | + } | |
| 119 | + | |
| 120 | + public void saveUrl(String urladdress) { | |
| 121 | + checkdoc(); | |
| 122 | + removeElement("url"); // 既にElementが存在してた場合、一度削除 | |
| 123 | + | |
| 124 | + Element url = document.createElement("url"); | |
| 125 | + url.appendChild(document.createTextNode(urladdress)); | |
| 126 | + root.appendChild(url); | |
| 127 | + | |
| 128 | + } | |
| 129 | + | |
| 130 | + public void saveSearchList(ArrayList slist) { | |
| 131 | + checkdoc(); | |
| 132 | + removeElement("searchlist"); // 既にElementが存在してた場合、一度削除 | |
| 133 | + | |
| 134 | + for (Object slist1 : slist) { | |
| 135 | + SearchData sdat = (SearchData) slist1; | |
| 136 | + | |
| 137 | + Element cslist = document.createElement("searchlist"); | |
| 138 | + | |
| 139 | + addChild(cslist, "item", sdat.getitem()); | |
| 140 | + addChild(cslist, "htmltag", sdat.getHtmltag()); | |
| 141 | + addChild(cslist, "htmlid", sdat.getHtmlid()); | |
| 142 | + addChild(cslist, "htmlclass", sdat.getHtmlclass()); | |
| 143 | + addChild(cslist, "around", sdat.getaround()); | |
| 144 | + addChild(cslist, "regexp", sdat.getregexp()); | |
| 145 | + | |
| 146 | + root.appendChild(cslist); | |
| 147 | + } | |
| 148 | + | |
| 149 | + } | |
| 150 | + | |
| 151 | + private void addChild(Element cslist, String keyword, String data) { | |
| 152 | + if(!data.isEmpty()) { | |
| 153 | + Element element = document.createElement(keyword); | |
| 154 | + element.appendChild(document.createTextNode(data)); | |
| 155 | + cslist.appendChild(element); | |
| 156 | + } | |
| 157 | + } | |
| 158 | + | |
| 159 | + private void removeElement(String elementTagName) { | |
| 160 | + NodeList nodelist = document.getElementsByTagName(elementTagName); | |
| 161 | + for(int i = 0; i < nodelist.getLength(); i++) { | |
| 162 | + Node node = nodelist.item(i); | |
| 163 | + root.removeChild(node); | |
| 164 | + } | |
| 165 | + } | |
| 166 | + | |
| 167 | + private void checkdoc() { | |
| 168 | + | |
| 169 | + if(document == null) { | |
| 170 | + DOMImplementation domImpl = builder.getDOMImplementation(); | |
| 171 | + document = domImpl.createDocument("","searchdata",null); | |
| 172 | + } | |
| 173 | + | |
| 174 | + root = document.getDocumentElement(); | |
| 175 | + | |
| 176 | + } | |
| 177 | + | |
| 178 | + public void read(File file) { | |
| 179 | + | |
| 180 | + try { | |
| 181 | + document = builder.parse(file); | |
| 182 | + root = document.getDocumentElement(); | |
| 183 | + | |
| 184 | + } catch (SAXException | IOException ex) { | |
| 185 | + Logger.getLogger(XmlSearchDataRW2.class.getName()).log(Level.SEVERE, null, ex); | |
| 186 | + } | |
| 187 | + | |
| 188 | + } | |
| 189 | + | |
| 190 | + public void write(File file) { | |
| 191 | + try { | |
| 192 | + TransformerFactory transFactory = TransformerFactory.newInstance(); | |
| 193 | + Transformer transformer = transFactory.newTransformer(); | |
| 194 | + | |
| 195 | + DOMSource source = new DOMSource(document); | |
| 196 | + FileOutputStream os = new FileOutputStream(file); | |
| 197 | + StreamResult result = new StreamResult(os); | |
| 198 | + transformer.transform(source, result); | |
| 199 | + | |
| 200 | + } catch (TransformerConfigurationException ex) { | |
| 201 | + Logger.getLogger(XmlSearchDataRW2.class.getName()).log(Level.SEVERE, null, ex); | |
| 202 | + } catch (FileNotFoundException | TransformerException ex) { | |
| 203 | + Logger.getLogger(XmlSearchDataRW2.class.getName()).log(Level.SEVERE, null, ex); | |
| 204 | + } | |
| 205 | + } | |
| 206 | + | |
| 207 | + | |
| 208 | +} |