作業部屋の使い方を試しています。
xml読み書き試行錯誤
| @@ -0,0 +1,99 @@ | ||
| 1 | +package XmlTest1; | |
| 2 | + | |
| 3 | +import javax.xml.parsers.*; | |
| 4 | +import org.w3c.dom.*; | |
| 5 | +import org.xml.sax.*; | |
| 6 | +import java.io.*; | |
| 7 | + | |
| 8 | +public class AllShow { | |
| 9 | + | |
| 10 | + /** | |
| 11 | + * 読み込むXMLファイル名 | |
| 12 | + */ | |
| 13 | + final static String uri = "example1.xml"; | |
| 14 | + | |
| 15 | + /** | |
| 16 | + * 主メソッド | |
| 17 | + */ | |
| 18 | + public void runner() { | |
| 19 | + DocumentBuilderFactory factory; | |
| 20 | + DocumentBuilder builder; | |
| 21 | + Node root; | |
| 22 | + Node child; | |
| 23 | + NodeList XMLdata; | |
| 24 | + try { | |
| 25 | + factory = DocumentBuilderFactory.newInstance(); | |
| 26 | + builder = factory.newDocumentBuilder(); | |
| 27 | + factory.setIgnoringElementContentWhitespace(true); | |
| 28 | + factory.setIgnoringComments(true); | |
| 29 | + factory.setValidating(true); | |
| 30 | + root = builder.parse(uri); | |
| 31 | + | |
| 32 | + showNodes(root, ""); | |
| 33 | + | |
| 34 | + } catch (ParserConfigurationException | SAXException | IOException e0) { | |
| 35 | + System.out.println(e0.getMessage()); | |
| 36 | + } | |
| 37 | + } | |
| 38 | + | |
| 39 | + /** | |
| 40 | + * nodeの子ノードを全て表示します。子ノードがない場合はなにも出力しません。 | |
| 41 | + * spaceはノードを表示する前の行頭のスペースです。 | |
| 42 | + * (再帰処理をしているので注意) | |
| 43 | + * @param node | |
| 44 | + * @param space | |
| 45 | + */ | |
| 46 | + public void showNodes(Node node, String space) { | |
| 47 | + NodeList nodes = node.getChildNodes(); | |
| 48 | + for (int i = 0; i < nodes.getLength(); i++) { | |
| 49 | + Node node2 = nodes.item(i); | |
| 50 | + System.out.println(space + "「" + node2.getNodeName() + "/" + node2.getNodeValue() + "/" + getTypeMes(node2) + "」"); | |
| 51 | + showNodes(node2, space + " "); | |
| 52 | + } | |
| 53 | + } | |
| 54 | + | |
| 55 | + /** | |
| 56 | + * ノードの種類を示します | |
| 57 | + * @param node | |
| 58 | + * @return | |
| 59 | + */ | |
| 60 | + public String getTypeMes(Node node) { | |
| 61 | + String s = null; | |
| 62 | + switch (node.getNodeType()) { | |
| 63 | + case Node.ATTRIBUTE_NODE: | |
| 64 | + s = "Attr"; | |
| 65 | + case Node.CDATA_SECTION_NODE: | |
| 66 | + s = "CDATASection"; | |
| 67 | + case Node.COMMENT_NODE: | |
| 68 | + s = "Comment"; | |
| 69 | + case Node.DOCUMENT_FRAGMENT_NODE: | |
| 70 | + s = "DocumentFragment"; | |
| 71 | + case Node.DOCUMENT_NODE: | |
| 72 | + s = "Document"; | |
| 73 | + case Node.DOCUMENT_TYPE_NODE: | |
| 74 | + s = "DocumentType"; | |
| 75 | + case Node.ELEMENT_NODE: | |
| 76 | + s = "Element"; | |
| 77 | + case Node.ENTITY_NODE: | |
| 78 | + s = "Entity"; | |
| 79 | + case Node.ENTITY_REFERENCE_NODE: | |
| 80 | + s = "EntityReference"; | |
| 81 | + case Node.NOTATION_NODE: | |
| 82 | + s = "Notation"; | |
| 83 | + case Node.PROCESSING_INSTRUCTION_NODE: | |
| 84 | + s = "ProcessingInstruction"; | |
| 85 | + case Node.TEXT_NODE: | |
| 86 | + s = "Text"; | |
| 87 | + } | |
| 88 | + return s; | |
| 89 | + } | |
| 90 | + | |
| 91 | + /** | |
| 92 | + * メイン | |
| 93 | + * @param argv | |
| 94 | + */ | |
| 95 | + public static void main(String[] argv) { | |
| 96 | + AllShow t = new AllShow(); | |
| 97 | + t.runner(); | |
| 98 | + } | |
| 99 | +} |
| @@ -0,0 +1,90 @@ | ||
| 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 | + | |
| 7 | +package XmlTest1; | |
| 8 | + | |
| 9 | +import java.io.File; | |
| 10 | +import java.io.IOException; | |
| 11 | +import java.util.logging.Level; | |
| 12 | +import java.util.logging.Logger; | |
| 13 | +import javax.xml.parsers.DocumentBuilder; | |
| 14 | +import javax.xml.parsers.DocumentBuilderFactory; | |
| 15 | +import javax.xml.parsers.ParserConfigurationException; | |
| 16 | +import org.w3c.dom.Document; | |
| 17 | +import org.w3c.dom.Element; | |
| 18 | +import org.w3c.dom.Node; | |
| 19 | +import org.w3c.dom.NodeList; | |
| 20 | +import org.xml.sax.SAXException; | |
| 21 | + | |
| 22 | +/** | |
| 23 | + * | |
| 24 | + * @author kgto | |
| 25 | + */ | |
| 26 | +public class XmlRead1 { | |
| 27 | + | |
| 28 | + /** | |
| 29 | + * @param args the command line arguments | |
| 30 | + */ | |
| 31 | + public static void main(String[] args) { | |
| 32 | + XmlRead1 a = new XmlRead1(); | |
| 33 | + a.getnode(); | |
| 34 | + } | |
| 35 | + | |
| 36 | + public void getnode() { | |
| 37 | + try { | |
| 38 | + // XML ドキュメントの読み込み | |
| 39 | + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); | |
| 40 | + DocumentBuilder builder = factory.newDocumentBuilder(); | |
| 41 | + File f = new File( "MyTest.gwt.xml" ); | |
| 42 | + Document doc = builder.parse( f ); | |
| 43 | + | |
| 44 | + // ルート要素の取得 | |
| 45 | + Element root = doc.getDocumentElement(); | |
| 46 | + // 子要素リストの取得 | |
| 47 | + NodeList children = root.getChildNodes(); | |
| 48 | + | |
| 49 | + nodePrint(doc); | |
| 50 | + | |
| 51 | + /* | |
| 52 | + // 子要素のタグ名を出力 | |
| 53 | + for(int i = 0; i < children.getLength(); i++) { | |
| 54 | + Node child = children.item(i); | |
| 55 | + if( child instanceof Element ) { | |
| 56 | + Element childElement = (Element) child; | |
| 57 | + System.out.println( childElement.getTagName() + " " | |
| 58 | + + childElement.getNodeName() + " " | |
| 59 | + + childElement.getNodeValue() | |
| 60 | + ); | |
| 61 | + | |
| 62 | + } | |
| 63 | + } | |
| 64 | + */ | |
| 65 | + | |
| 66 | + } catch (ParserConfigurationException | IOException | SAXException ex) { | |
| 67 | + Logger.getLogger(XmlRead1.class.getName()).log(Level.SEVERE, null, ex); | |
| 68 | + } | |
| 69 | + } | |
| 70 | + | |
| 71 | + public void nodePrint(Node element) { | |
| 72 | + | |
| 73 | + NodeList nodes = element.getChildNodes(); | |
| 74 | + for (int i = 0; i < nodes.getLength(); i++) { | |
| 75 | + Node node2 = nodes.item(i); | |
| 76 | + if(node2 instanceof Element) { | |
| 77 | + Element childElement = (Element)node2; | |
| 78 | + //System.out.println(childElement.hasChildNodes()); | |
| 79 | + System.out.println(childElement.getNodeName() + " = " + | |
| 80 | + childElement.getNodeValue() + " " + | |
| 81 | + childElement.getNodeType() | |
| 82 | + ); | |
| 83 | + nodePrint(childElement); | |
| 84 | + } | |
| 85 | + | |
| 86 | + } | |
| 87 | + | |
| 88 | + } | |
| 89 | + | |
| 90 | +} |
| @@ -0,0 +1,41 @@ | ||
| 1 | + | |
| 2 | +package XmlTest1; | |
| 3 | + | |
| 4 | +import java.io.IOException; | |
| 5 | +import javax.xml.parsers.DocumentBuilder; | |
| 6 | +import javax.xml.parsers.DocumentBuilderFactory; | |
| 7 | +import javax.xml.parsers.ParserConfigurationException; | |
| 8 | +import org.w3c.dom.Document; | |
| 9 | +import org.w3c.dom.Node; | |
| 10 | +import org.xml.sax.SAXException; | |
| 11 | + | |
| 12 | +public class Traversal { | |
| 13 | + | |
| 14 | + public static void main(String[] args) | |
| 15 | + throws ParserConfigurationException, SAXException, IOException { | |
| 16 | + //パーサを作成 | |
| 17 | + DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); | |
| 18 | + DocumentBuilder db = dbf.newDocumentBuilder(); | |
| 19 | + //パース | |
| 20 | + Document doc = db.parse("MyTest.gwt.xml"); | |
| 21 | + print(doc, 0); | |
| 22 | + } | |
| 23 | + | |
| 24 | + static void print(Node node, int indent) { | |
| 25 | + // インデントを表示 | |
| 26 | + for (int i = 0; i < indent; i++) { | |
| 27 | + System.out.print(" "); | |
| 28 | + } | |
| 29 | + // ノードの種別と名前、値を出力 | |
| 30 | + System.out.println(node.getNodeType() + " " + node.getNodeName() + " = " + node.getNodeValue()); | |
| 31 | + if(node.getNodeType() == Node.ELEMENT_NODE) { | |
| 32 | + System.out.println("FistValue = " + node.getFirstChild().getNodeValue()); | |
| 33 | + } | |
| 34 | + | |
| 35 | + Node child; | |
| 36 | + for (child = node.getFirstChild(); child != null; child = child.getNextSibling()) { | |
| 37 | + // 再帰呼び出し | |
| 38 | + print(child, indent + 1); | |
| 39 | + } | |
| 40 | + } | |
| 41 | +} |
| @@ -0,0 +1,20 @@ | ||
| 1 | +<?xml version="1.0" encoding="UTF-8"?> | |
| 2 | +<searchdata> | |
| 3 | + <url>対象HPアドレス</url> | |
| 4 | + <searchlist> | |
| 5 | + <item>項目名</item> | |
| 6 | + <htmltag>タグ</htmltag> | |
| 7 | + <htmlid>ID</htmlid> | |
| 8 | + <htmlclass>クラス</htmlclass> | |
| 9 | + <around>位置</around> | |
| 10 | + <regexp>抽出条件</regexp> | |
| 11 | + </searchlist> | |
| 12 | + <searchlist> | |
| 13 | + <item>項目名</item> | |
| 14 | + <htmltag>タグ</htmltag> | |
| 15 | + <htmlid>ID</htmlid> | |
| 16 | + <htmlclass>クラス</htmlclass> | |
| 17 | + <around>位置</around> | |
| 18 | + <regexp>抽出条件</regexp> | |
| 19 | + </searchlist> | |
| 20 | +</searchdata> |
| @@ -0,0 +1,12 @@ | ||
| 1 | +<?xml version="1.0" encoding="UTF-8" ?> | |
| 2 | +<venture> | |
| 3 | + <company> | |
| 4 | + <name>ソフトバンク株式会社</name> | |
| 5 | + <url>http://www.softbank.co.jp/</url> | |
| 6 | + </company> | |
| 7 | + | |
| 8 | + <company> | |
| 9 | + <name>楽天株式会社</name> | |
| 10 | + <url>http://www.rakuten.co.jp/info/</url> | |
| 11 | + </company> | |
| 12 | +</venture> |