XML内の特定のNodeを文字列で取得する

概要

XML内の特定のNodeを文字列で取得する方法です。

サンプルコード

XMLファイル

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
 <employees>
  <employee>太郎</employee>
  <employee>次郎</employee>
  <employee>𩸽️</employee>
</employees>

実行結果

<employee>太郎</employee><employee>次郎</employee><employee>𩸽️</employee>

動作環境

参考サイト

XMLの子ども要素を丸ごとStringで取得する方法 − Java Solution − @IT

Stream APIでリストを操作する

概要

Stream APIを使用してリストを操作する方法です。

サンプルコード

実行結果

[id=0の項目を取得する(全て)]
Employee [id=0, name=Adam]
Employee [id=0, name=Abel]
[id, nameの昇順にソートする]
Employee [id=0, name=Abel]
Employee [id=0, name=Adam]
Employee [id=1, name=Cliff]
Employee [id=2, name=Brian]
Employee [id=2, name=Brian]
[重複を排除する]
Employee [id=0, name=Adam]
Employee [id=2, name=Brian]
Employee [id=1, name=Cliff]
Employee [id=0, name=Abel]

動作環境

文字列を指定したバイト数に切り詰める

概要

文字列を指定したバイト数に切り詰める方法です。
マルチバイト文字の途中となる場合も、その前の文字まで切り出します。

サンプルコード

実行結果

あいうえ
あいうえお
あいうえお

動作環境

Selenium IDEでブラウザ操作を記録する

概要

Selenium IDEでブラウザ操作を記録する方法です。 以下の2つを利用します。

  • Selenium IDE - ブラウザ操作を記録し、テストコードを出力する。
  • Selenium WebDriver - 出力したテストコードを実行するためのライブラリ。

Selenium IDE のインストール

Firefoxのアドオンからインストールします。 addons.mozilla.org

ブラウザ操作の記録

  1. Firefoxの右上に追加された[Selenium IDE]ボタンを押下し、Selenium IDEを起動する。
  2. 記録したいブラウザの操作を行う。
  3. [ファイル] - [テストケースをエクスポート] - [Java / JUnit4 / WebDriver]から、テストコードを出力する。

テストコード

package com.example.tests;

import static org.junit.Assert.fail;

import java.util.concurrent.TimeUnit;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.NoAlertPresentException;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class SeleniumTest {
    private WebDriver driver;
    private String baseUrl;
    private boolean acceptNextAlert = true;
    private StringBuffer verificationErrors = new StringBuffer();

    @Before
    public void setUp() throws Exception {
        driver = new FirefoxDriver();
        baseUrl = "https://www.google.co.jp/";
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    }

    @Test
    public void testSeleniumT() throws Exception {
        // Googleからはてなを表示
        driver.get(baseUrl
                + "/search?q=%E3%81%AF%E3%81%A6%E3%81%AA&ie=utf-8&oe=utf-8&hl=ja");
        driver.findElement(By.linkText("はてな")).click();
    }

    @After
    public void tearDown() throws Exception {
        driver.quit();
        String verificationErrorString = verificationErrors.toString();
        if (!"".equals(verificationErrorString)) {
            fail(verificationErrorString);
        }
    }

    private boolean isElementPresent(By by) {
        try {
            driver.findElement(by);
            return true;
        } catch (NoSuchElementException e) {
            return false;
        }
    }

    private boolean isAlertPresent() {
        try {
            driver.switchTo().alert();
            return true;
        } catch (NoAlertPresentException e) {
            return false;
        }
    }

    private String closeAlertAndGetItsText() {
        try {
            Alert alert = driver.switchTo().alert();
            String alertText = alert.getText();
            if (acceptNextAlert) {
                alert.accept();
            } else {
                alert.dismiss();
            }
            return alertText;
        } finally {
            acceptNextAlert = true;
        }
    }
}

動作環境

MacのEclipseをclean起動するコマンド

概要

MacEclipseをclean起動するコマンドです。

実行コマンド

$ /Applications/eclipse/Eclipse.app/Contents/MacOS/eclipse -clean -clearPersistedState
  • Eclipseをsudoで起動してしまい、動かなくなった時の対処方法
$ sudo chown -R 自分のユーザ名 /Applications/eclipse
$ chmod -R g-w /Applications/eclipse

参考サイト

Linuxコマンド集 - 【 chown 】 ファイルやディレクトリの所有者を変更する:ITpro
Linuxコマンド集 - 【 chmod 】 ファイルやディレクトリのアクセス権を変更する:ITpro