ユーザ用ツール

サイト用ツール


programming:java

差分

このページの2つのバージョン間の差分を表示します。

この比較画面へのリンク

両方とも前のリビジョン前のリビジョン
次のリビジョン
前のリビジョン
programming:java [2013/08/20 06:00] dotprogramming:java [2015/06/17 05:20] (現在) – 外部編集 127.0.0.1
行 61: 行 61:
         } catch (IOException e) {         } catch (IOException e) {
             e.printStackTrace();             e.printStackTrace();
-        } 
-    } 
-} 
-</file> 
- 
-===== 正規表現を用いたパターンマッチ ===== 
- 
-<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 = "Hello, World"; 
- 
-        Matcher matcher = Pattern.compile("(.+?), (.+?)$").matcher(str); 
-        if (matcher.find()){ 
-            System.out.println(matcher.group(1)); //Hello 
-            System.out.println(matcher.group(2)); //World 
         }         }
     }     }
行 106: 行 87:
 } }
 </file> </file>
- 
  
 <file ini application.ini> <file ini application.ini>
行 113: 行 93:
 </file> </file>
  
 +
 +===== 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("test.xml");
 +        String expression = "//test/unko[@id='hello']";
 +
 +        try {
 +            NodeList nodes = (NodeList) xpath.evaluate(expression, inputSource, XPathConstants.NODESET);
 +            for (int i = 0; i < nodes.getLength(); i++) {
 +                Node node = nodes.item(i);
 +                String str = node.getTextContent();
 +                System.out.println(str);
 +            }
 +        } catch (XPathExpressionException e) {
 +            e.printStackTrace();
 +        }
 +    }
 +}
 +</file>
 +
 +<file xml test.xml>
 +<?xml version="1.0" encoding="UTF-8"?>
 +<test>
 +  <unko id="hello">001</unko>
 +  <unko id="world">002</unko>
 +</test>
 +</file>
 +
 +===== 正規表現を用いたパターンマッチ =====
 +
 +<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 = "Hello, World";
 +
 +        Matcher matcher = Pattern.compile("(.+?), (.+?)$").matcher(str);
 +        if (matcher.find()){
 +            System.out.println(matcher.group(1)); //Hello
 +            System.out.println(matcher.group(2)); //World
 +        }
 +    }
 +}
 +</file>
 +
 +===== 正規表現を用いた文字列置換 =====
 +
 +<file java RegexReplaceString.java>
 +import java.util.regex.Pattern;
 +
 +public class RegexReplaceString{
 +    public static void main(String[] args) {
 +        String str = "Hello, unko";
 +
 +        // unko -> World
 +        System.out.println(Pattern.compile("(unko)$").matcher(str).replaceFirst("World"));
 +
 +        // unko -> World
 +        System.out.println(str.replaceAll("(unko)$", "World"));
 +    }
 +}
 +</file>
  
 ===== 拡張子指定ファイル一覧 ===== ===== 拡張子指定ファイル一覧 =====
行 182: 行 240:
 </file> </file>
  
-===== 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[]){
-        XPath xpath XPathFactory.newInstance().newXPath(); +        int intType 10
-        InputSource inputSource = new InputSource("test.xml")+        String stringType = "10";
-        String expression = "//test/unko[@id='hello']";+
  
-        try { +        // int -> String 
-            NodeList nodes = (NodeList) xpath.evaluate(expression, inputSource, XPathConstants.NODESET); +        String Integer.toString(intType); 
-            for (int i = 0; i < nodes.getLength(); i++) { + 
-                Node node = nodes.item(i); +        // String -> int 
-                String str node.getTextContent(); +        int b = Integer.parseInt(stringType);
-                System.out.println(str); +
-            } +
-        } catch (XPathExpressionException e) { +
-            e.printStackTrace(); +
-        }+
     }     }
 } }
 </file> </file>
  
-<file xml test.xml> 
-<?xml version="1.0" encoding="UTF-8"?> 
-<test> 
-  <unko id="hello">001</unko> 
-  <unko id="world">002</unko> 
-</test> 
-</file> 
programming/java.1376978433.txt.gz · 最終更新: 2015/06/17 05:09 (外部編集)