programming:java
差分
このページの2つのバージョン間の差分を表示します。
| 両方とも前のリビジョン前のリビジョン次のリビジョン | 前のリビジョン | ||
| programming:java [2013/08/20 06:00] – dot | programming:java [2015/06/17 05:20] (現在) – 外部編集 127.0.0.1 | ||
|---|---|---|---|
| 行 61: | 行 61: | ||
| } catch (IOException e) { | } catch (IOException e) { | ||
| e.printStackTrace(); | e.printStackTrace(); | ||
| - | } | ||
| - | } | ||
| - | } | ||
| - | </ | ||
| - | |||
| - | ===== 正規表現を用いたパターンマッチ ===== | ||
| - | |||
| - | <file java RegexPatternMatch.java> | ||
| - | import java.util.regex.Matcher; | ||
| - | import java.util.regex.Pattern; | ||
| - | |||
| - | public class RegexPatternMatch{ | ||
| - | public static void main(String[] args) { | ||
| - | String str = " | ||
| - | |||
| - | Matcher matcher = Pattern.compile(" | ||
| - | if (matcher.find()){ | ||
| - | System.out.println(matcher.group(1)); | ||
| - | System.out.println(matcher.group(2)); | ||
| } | } | ||
| } | } | ||
| 行 106: | 行 87: | ||
| } | } | ||
| </ | </ | ||
| - | |||
| <file ini application.ini> | <file ini application.ini> | ||
| 行 113: | 行 93: | ||
| </ | </ | ||
| + | |||
| + | ===== XPathでXMLの読み込み ===== | ||
| + | |||
| + | <file java UseXPath.java> | ||
| + | |||
| + | import javax.xml.xpath.XPath; | ||
| + | import javax.xml.xpath.XPathConstants; | ||
| + | import javax.xml.xpath.XPathExpressionException; | ||
| + | import javax.xml.xpath.XPathFactory; | ||
| + | |||
| + | import org.w3c.dom.Node; | ||
| + | import org.w3c.dom.NodeList; | ||
| + | import org.xml.sax.InputSource; | ||
| + | |||
| + | public class UseXPath{ | ||
| + | public static void main(String args[]){ | ||
| + | XPath xpath = XPathFactory.newInstance().newXPath(); | ||
| + | InputSource inputSource = new InputSource(" | ||
| + | String expression = "// | ||
| + | |||
| + | try { | ||
| + | NodeList nodes = (NodeList) xpath.evaluate(expression, | ||
| + | for (int i = 0; i < nodes.getLength(); | ||
| + | Node node = nodes.item(i); | ||
| + | String str = node.getTextContent(); | ||
| + | System.out.println(str); | ||
| + | } | ||
| + | } catch (XPathExpressionException e) { | ||
| + | e.printStackTrace(); | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | <file xml test.xml> | ||
| + | <?xml version=" | ||
| + | < | ||
| + | <unko id=" | ||
| + | <unko id=" | ||
| + | </ | ||
| + | </ | ||
| + | |||
| + | ===== 正規表現を用いたパターンマッチ ===== | ||
| + | |||
| + | <file java RegexPatternMatch.java> | ||
| + | import java.util.regex.Matcher; | ||
| + | import java.util.regex.Pattern; | ||
| + | |||
| + | public class RegexPatternMatch{ | ||
| + | public static void main(String[] args) { | ||
| + | String str = " | ||
| + | |||
| + | Matcher matcher = Pattern.compile(" | ||
| + | if (matcher.find()){ | ||
| + | System.out.println(matcher.group(1)); | ||
| + | System.out.println(matcher.group(2)); | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | ===== 正規表現を用いた文字列置換 ===== | ||
| + | |||
| + | <file java RegexReplaceString.java> | ||
| + | import java.util.regex.Pattern; | ||
| + | |||
| + | public class RegexReplaceString{ | ||
| + | public static void main(String[] args) { | ||
| + | String str = " | ||
| + | |||
| + | // unko -> World | ||
| + | System.out.println(Pattern.compile(" | ||
| + | |||
| + | // unko -> World | ||
| + | System.out.println(str.replaceAll(" | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| ===== 拡張子指定ファイル一覧 ===== | ===== 拡張子指定ファイル一覧 ===== | ||
| 行 182: | 行 240: | ||
| </ | </ | ||
| - | ===== XPathでXMLの読み込み ===== | ||
| - | <file java UseXPath.java> | + | ===== 型変換 ===== |
| - | import javax.xml.xpath.XPath; | + | <file java TypeCast.java> |
| - | import javax.xml.xpath.XPathConstants; | + | public class TypeCast |
| - | import javax.xml.xpath.XPathExpressionException; | + | |
| - | import javax.xml.xpath.XPathFactory; | + | |
| - | + | ||
| - | import org.w3c.dom.Node; | + | |
| - | import org.w3c.dom.NodeList; | + | |
| - | import org.xml.sax.InputSource; | + | |
| - | + | ||
| - | public class UseXPath{ | + | |
| public static void main(String args[]){ | public static void main(String args[]){ | ||
| - | | + | |
| - | InputSource inputSource = new InputSource(" | + | String |
| - | String | + | |
| - | | + | |
| - | NodeList nodes = (NodeList) xpath.evaluate(expression, | + | String |
| - | for (int i = 0; i < nodes.getLength(); | + | |
| - | Node node = nodes.item(i); | + | // String -> int |
| - | | + | |
| - | | + | |
| - | } | + | |
| - | | + | |
| - | e.printStackTrace(); | + | |
| - | } | + | |
| } | } | ||
| } | } | ||
| </ | </ | ||
| - | <file xml test.xml> | ||
| - | <?xml version=" | ||
| - | < | ||
| - | <unko id=" | ||
| - | <unko id=" | ||
| - | </ | ||
| - | </ | ||
programming/java.1376978433.txt.gz · 最終更新: (外部編集)
