<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Yukun&#039;s Blog &#187; XML</title>
	<atom:link href="http://www.yukun.info/blog/tag/xml/feed" rel="self" type="application/rss+xml" />
	<link>http://www.yukun.info</link>
	<description>難しいことは分かりやすく、簡単なことは面白く紹介</description>
	<lastBuildDate>Thu, 26 Jan 2012 03:33:59 +0000</lastBuildDate>
	<language>ja</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>ActionScript: XMLの子要素の読み込み &#8211; XMLオブジェクトと「.. 」「[]」演算子</title>
		<link>http://www.yukun.info/blog/2008/10/actionscript-xml-element-attribute.html</link>
		<comments>http://www.yukun.info/blog/2008/10/actionscript-xml-element-attribute.html#comments</comments>
		<pubDate>Mon, 06 Oct 2008 20:50:34 +0000</pubDate>
		<dc:creator>yukun</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">http://www.yukun.info/?p=1207</guid>
		<description><![CDATA[XMLのパースの練習に、AmazonのWebサービスAPIを用いて本を検索し、表紙の縮小画像をStage上にランダムに配置するFlashをAS3で書いてみました。 XMLの扱い方の一例 以下は上のFlashの解説ではあり &#8230; <a href="http://www.yukun.info/blog/2008/10/actionscript-xml-element-attribute.html">Continue reading <span class="meta-nav">&#8594;</span></a><p><a href="http://www.yukun.info/blog/2008/10/actionscript-xml-element-attribute.html">ActionScript: XMLの子要素の読み込み &#8211; XMLオブジェクトと「.. 」「[]」演算子</a> is a post from: <a href="http://www.yukun.info">Yukun&#039;s Blog</a></p>
]]></description>
			<content:encoded><![CDATA[<p>XMLのパースの練習に、AmazonのWebサービスAPIを用いて本を検索し、表紙の縮小画像をStage上にランダムに配置するFlashをAS3で書いてみました。<br />
<span id="more-1207"></span><br />
<!-- Copyright (C) yukun. --><!-- http://www.yukun.info/ --><embed src="http://www.yukun.info/downloads/amazo_search01.swf" loop="false" quality="high" bgcolor="#ffffff" width="400" height="400"></p>
<h2>XMLの扱い方の一例</h2>
<p>以下は上のFlashの解説ではありませんが、作成過程でXMLの扱いで詰ったところがあったのでピックアップしてみます。</p>
<h3>使用するXMLデータ</h3>
<p>通常は外部ファイルを読み込みますが下の例ではXMLデータをソースコード中に書き込んでいます。<br />
まず、扱うXMLデータは、</p>
<pre>
&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;books&gt;
  &lt;book&gt;
    &lt;title&gt;&lt;![CDATA[新版 明解C言語 入門編]]&gt;&lt;/title&gt;
    &lt;author&gt;&lt;![CDATA[柴田望洋]]&gt;&lt;/author&gt;
    &lt;image&gt;http://ecx.images-amazon.com/images/I/51N3ym-NBRL._SL75_.jpg&lt;/image&gt;
  &lt;/book&gt;
  &lt;book&gt;
    &lt;title&gt;&lt;![CDATA[プログラミング言語C ANSI規格準拠]]&gt;&lt;/title&gt;
    &lt;author&gt;&lt;![CDATA[B.W. カーニハン]]&gt;&lt;/author&gt;
    &lt;image&gt;http://ecx.images-amazon.com/images/I/41W69WGATNL._SL75_.jpg&lt;/image&gt;
  &lt;/book&gt;
  &lt;book&gt;
    &lt;title&gt;&lt;![CDATA[やさしいC++―まずは「C言語」からはじめよう!! (I・O BOOKS)]]&gt;&lt;/title&gt;
    &lt;author&gt;&lt;![CDATA[米村 貴裕]]&gt;&lt;/author&gt;
    &lt;image&gt;http://ecx.images-amazon.com/images/I/51PKHX9QEVL._SL75_.jpg&lt;/image&gt;
  &lt;/book&gt;
&lt;/books&gt;
</pre>
<p>とします。本のデータはAmazonで検索語を「C言語」にして検索した結果を用いています。<br />
ちなみに、</p>
<pre>&lt;![CDATA[ホニャララ]]&gt;</pre>
<p>タグで囲まれた要素内では全ての文字列を扱うことが出来ます。</p>
<h3>外部XMLデータからXMLオブジェクトを作成</h3>
<p>大抵は外部ファイルから読み込みます。その場合は、URLLoaderクラスのdataフィールドをXMLのコンストラクタの引数に渡せばOKです。</p>
<pre>
var urlLoader:URLLoader = new URLLoader();
urlLoader.load("http://www.example.com/text.xml");
// 中略：ロードが完了(のイベントをキャッチ)したら↓
var booksXml:XML = new XML(urlLoader.data);
</pre>
<p>ここではbooksXml変数がXMLオブジェクトです。</p>
<h3>ソースに直接XMLデータを書き込む場合</h3>
<p>あまりそういった場合は無いのですが、テストなどの際は</p>
<pre>
var booksXml:XML = &lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;books&gt;
	&lt;book&gt;
		&lt;title&gt;&lt;![CDATA[新版 明解C言語 入門編]]&gt;&lt;/title&gt;
		&lt;author&gt;&lt;![CDATA[柴田望洋]]&gt;&lt;/author&gt;

＜中略＞

	&lt;/book&gt;
&lt;/books&gt;;
</pre>
<p>のように書きます。他にもXMLコンストラクタに文字列リテラルとして渡すことも可能です。その場合、渡す文字列は一行にします。</p>
<h3>XMLの各要素と属性へのアクセス</h3>
<p>今回扱うXMLの入れ子の深さ2ですかね。booksタグの中にbook要素があり、そのbookタグの中にtitle、author、image要素があり、それぞれのタグの中にほしいデータがある、と。</p>
<p>XMLオブジェクトにデータを代入しインスタンスを作成した際に、パースは終わっていますので子要素へのアクセスは「.」、子孫へは「..」や「[]」演算子を用います。また、子(子孫)属性に対しては「.@」「..@」です。</p>
<pre>
trace(booksXml.book); // 実行結果A (booksXml['book']と同値)
trace(booksXml.book.title); // 実行結果B (xml['book']["title"]やbooksXml..titleと同値)
</pre>
<h4>実行結果A</h4>
<pre>
  &lt;book&gt;
    &lt;title&gt;&lt;![CDATA[新版 明解C言語 入門編]]&gt;&lt;/title&gt;
    &lt;author&gt;&lt;![CDATA[柴田望洋]]&gt;&lt;/author&gt;
    &lt;image&gt;http://ecx.images-amazon.com/images/I/51N3ym-NBRL._SL75_.jpg&lt;/image&gt;
  &lt;/book&gt;
  &lt;book&gt;
    &lt;title&gt;&lt;![CDATA[プログラミング言語C ANSI規格準拠]]&gt;&lt;/title&gt;
    &lt;author&gt;&lt;![CDATA[B.W. カーニハン]]&gt;&lt;/author&gt;
    &lt;image&gt;http://ecx.images-amazon.com/images/I/41W69WGATNL._SL75_.jpg&lt;/image&gt;
  &lt;/book&gt;
  &lt;book&gt;
    &lt;title&gt;&lt;![CDATA[やさしいC++―まずは「C言語」からはじめよう!! (I・O BOOKS)]]&gt;&lt;/title&gt;
    &lt;author&gt;&lt;![CDATA[米村 貴裕]]&gt;&lt;/author&gt;
    &lt;image&gt;http://ecx.images-amazon.com/images/I/51PKHX9QEVL._SL75_.jpg&lt;/image&gt;
  &lt;/book&gt;
</pre>
<h4>実行結果B</h4>
<pre>
&lt;title&gt;&lt;![CDATA[新版 明解C言語 入門編]]&gt;&lt;/title&gt;
&lt;title&gt;&lt;![CDATA[プログラミング言語C ANSI規格準拠]]&gt;&lt;/title&gt;
&lt;title&gt;&lt;![CDATA[やさしいC++―まずは「C言語」からはじめよう!! (I・O BOOKS)]]&gt;&lt;/title&gt;
</pre>
<p>一致する全ての要素をXMLList形式で返しています。実際のプログラムではfor文で回してデータを取得します。<br />
上で使った演算子に代わるメソッド等もありますが、それらはまたの機会に取り上げてみます。<br />
<h4>関連すると思われる記事：</h4>
<ul class="similar-posts">
<li><a href="http://www.yukun.info/blog/2008/10/actionscript-embed-mp3-sound-swf-flash.html" rel="bookmark" title="2008年10月8日">ActionScript: Flash(*.swf)にmp3ファイルを埋め込む &#8211; Embedタグ</a></li>
<li><a href="http://www.yukun.info/blog/2008/11/actionscript-flash-load-server-image-file.html" rel="bookmark" title="2008年11月21日">ActionScript: 画像ファイルをダウンロードして表示 &#8211; Loaderクラス</a></li>
<li><a href="http://www.yukun.info/blog/2008/11/actionscript-paint-flash-air-brush.html" rel="bookmark" title="2008年11月4日">AS3でお絵かきFlashを作る (5)エアーブラシ機能</a></li>
<li><a href="http://www.yukun.info/blog/2008/09/actionscript-mind-map-flash-node-drug-drop.html" rel="bookmark" title="2008年9月21日">AS3でMind MapぽいFlashを作る (2)各ノードのドラック＆ドロップ</a></li>
<li><a href="http://www.yukun.info/blog/2008/08/actionscript-mouse-event-listener.html" rel="bookmark" title="2008年8月20日">ActionScript: マウスをイベントリスナーに登録</a></li>
</ul>
<p><!-- Similar Posts took 11.193 ms --></p>
<p><a href="http://www.yukun.info/blog/2008/10/actionscript-xml-element-attribute.html">ActionScript: XMLの子要素の読み込み &#8211; XMLオブジェクトと「.. 」「[]」演算子</a> is a post from: <a href="http://www.yukun.info">Yukun&#039;s Blog</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.yukun.info/blog/2008/10/actionscript-xml-element-attribute.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Python: lxmlのインストール方法</title>
		<link>http://www.yukun.info/blog/2008/06/install-lxml.html</link>
		<comments>http://www.yukun.info/blog/2008/06/install-lxml.html#comments</comments>
		<pubDate>Thu, 12 Jun 2008 15:00:00 +0000</pubDate>
		<dc:creator>yukun</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[Module]]></category>
		<category><![CDATA[Setting]]></category>
		<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">http://www.yukun.info/trump/19700101/lxml%e3%81%ae%e3%82%a4%e3%83%b3%e3%82%b9%e3%83%88%e3%83%bc%e3%83%ab%e6%96%b9%e6%b3%95</guid>
		<description><![CDATA[lxml とはXMLやHTMLを扱うPythonのライブラリの一つです。 lxml is the most feature-rich and easy-to-use library for working with XM &#8230; <a href="http://www.yukun.info/blog/2008/06/install-lxml.html">Continue reading <span class="meta-nav">&#8594;</span></a><p><a href="http://www.yukun.info/blog/2008/06/install-lxml.html">Python: lxmlのインストール方法</a> is a post from: <a href="http://www.yukun.info">Yukun&#039;s Blog</a></p>
]]></description>
			<content:encoded><![CDATA[<p>lxml とはXMLやHTMLを扱うPythonのライブラリの一つです。</p>
<blockquote><p>lxml is the most feature-rich and easy-to-use library for working with XML and HTML in the Python language.<br />
 &#8211; <span style="font-style:italic;"><a href="http://codespeak.net/lxml/" target="_blank">lxml</a></span>の冒頭の文より</p></blockquote>
<h3>linux系OS(Fedoraなど)の場合</h3>
<pre>
# yum python-lxml
</pre>
<p>も一つの手ですがバージョンが古いので、通常はeasy_install経由でlxmlをインストールします。</p>
<p>前段階として、easy_installをインストールするために<a href="http://peak.telecommunity.com/dist/ez_setup.py" title="easy_installコマンドのインストール">http://peak.telecommunity.com/dist/ez_setup.py</a>をダウンロードしてスーパーユーザで実行します。</p>
<pre>
# python ez_setup.py error: invalid Python installation: unable to open /usr/lib/python2.5/config/Makefile (No such file or directory)
</pre>
<p>上のようなエラーがでた場合はpython-develをインストールします。また、合わせてlxmlに必要なパッケージもインストールするには以下のようなコマンドを実行します。</p>
<pre>
# yum install python-devel libxml2* libxslt*
</pre>
<p>改めて、</p>
<pre>
# python ez_setup.py
</pre>
<p>完了後、easy_installコマンドが実行できるようになります。<br />
lxmlのインストールは以下のコマンドを実行すればOKです。</p>
<pre>
# easy_install lxml
</pre>
<p>ライブラリの確認方法は、</p>
<pre>
$ python
>>> import lxml
>>>
</pre>
<p>でエラーが出なければOKです。</p>
<h3>アンインストール方法</h3>
<p>以下のコマンドを実行。</p>
<pre>
# easy_install -mxN lxml
</pre>
<h3>Windowsの場合</h3>
<p>Webアプリのデプロイ環境はLinux系だけれど、開発やテストの一部はWindows機で行いたいのでWindowsにeasy_installでインストールしようとしましたが、途中でエラーが出て中々上手くいきませんでした。</p>
<p>解決に時間が掛かりそうだったので、とりあえず<a href="http://pypi.python.org/pypi/lxml/1.3.4">Python Package Index : lxml 1.3.4</a>のlxml-1.3.4.win32-py2.5.exeをダウンロード＆インストールして済ませました。</p>
<h3>参考にした記事</h3>
<ul>
<li><a href="http://d.hatena.ne.jp/kuma8/20070712/1184247598" target="_blank">lxmlがインストールできない &#8211; kuma8の日記</a></li>
</ul>
<h4>関連すると思われる記事：</h4>
<ul class="similar-posts">
<li><a href="http://www.yukun.info/blog/2011/03/python-twitter-install.html" rel="bookmark" title="2011年3月5日">Python Twitter のインストール</a></li>
<li><a href="http://www.yukun.info/blog/2008/07/python-command-line-arguments.html" rel="bookmark" title="2008年7月26日">Python: コマンドライン引数の取得 &#8211; sys.argv変数</a></li>
<li><a href="http://www.yukun.info/blog/2008/09/fedora-yum-install-gnome.html" rel="bookmark" title="2008年9月15日">FedoraにGUI環境GNOMEをyumでインストール</a></li>
<li><a href="http://www.yukun.info/blog/2008/09/python-add-line-number-to-text-file.html" rel="bookmark" title="2008年9月8日">Python: テキストファイルの行頭に行番号を追加</a></li>
<li><a href="http://www.yukun.info/blog/2010/03/java-install-sen-windows.html" rel="bookmark" title="2010年3月25日">Java: 形態素解析Senをインストール(Windows編)</a></li>
</ul>
<p><!-- Similar Posts took 7.342 ms --></p>
<p><a href="http://www.yukun.info/blog/2008/06/install-lxml.html">Python: lxmlのインストール方法</a> is a post from: <a href="http://www.yukun.info">Yukun&#039;s Blog</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.yukun.info/blog/2008/06/install-lxml.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>イースト辞書Webサービスを利用したC#(with .Net)クライアントプログラミング</title>
		<link>http://www.yukun.info/blog/2008/01/csharp-web-service-client.html</link>
		<comments>http://www.yukun.info/blog/2008/01/csharp-web-service-client.html#comments</comments>
		<pubDate>Tue, 01 Jan 2008 15:00:00 +0000</pubDate>
		<dc:creator>yukun</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[C Sharp]]></category>
		<category><![CDATA[GUI]]></category>
		<category><![CDATA[Network]]></category>
		<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">http://www.yukun.info/trump/19700101/%e3%82%a4%e3%83%bc%e3%82%b9%e3%83%88%e8%be%9e%e6%9b%b8web%e3%82%b5%e3%83%bc%e3%83%93%e3%82%b9%e3%82%92%e5%88%a9%e7%94%a8%e3%81%97%e3%81%9fcwith-net%e3%82%af%e3%83%a9%e3%82%a4%e3%82%a2%e3%83%b3</guid>
		<description><![CDATA[C#からWebサービスを扱う練習をしてみました。例として、イースト辞書Webサービスを利用しようと思い宇宙仮面の C# プログラミングのこちらのページのソースコードを参考にしました(謝々)。 SOAP版APIの最新バージ &#8230; <a href="http://www.yukun.info/blog/2008/01/csharp-web-service-client.html">Continue reading <span class="meta-nav">&#8594;</span></a><p><a href="http://www.yukun.info/blog/2008/01/csharp-web-service-client.html">イースト辞書Webサービスを利用したC#(with .Net)クライアントプログラミング</a> is a post from: <a href="http://www.yukun.info">Yukun&#039;s Blog</a></p>
]]></description>
			<content:encoded><![CDATA[<p>C#からWebサービスを扱う練習をしてみました。例として、<a href="http://www.btonic.com/ws/index.html">イースト辞書Webサービス</a>を利用しようと思い<a href="http://uchukamen.com/">宇宙仮面の C# プログラミング</a>の<a href="http://uchukamen.com/Programming4/WebDict/index.htm">こちらのページ</a>のソースコードを参考にしました(謝々)。<br />
SOAP版APIの最新バージョンがv10になり、仕様が変更になったので以下にそれに対応したソースコードを示します。</p>
<h3>実行結果</h3>
<p><a href="http://www.yukun.info/wp-content/uploads/re_webdict01.png"><img src="http://www.yukun.info/wp-content/uploads/re_webdict01.png" alt="C#でWeb辞書サービス" title="C#でWeb辞書サービス" width="370" height="400" class="alignnone size-full wp-image-1990" /></a></p>
<h2>ソースコード</h2>
<h3>Form1.cs</h3>
<pre>
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using netdictist.jp.co.est.btonic; // Webサービスの名前空間を追加

namespace netdictist
{
  public partial class Form1 : Form
  {
    public Form1()
    {
      InitializeComponent();
    }

    private NetDicV10 netDicV10 = null; // Web Service インスタンス格納用変数
    private DicInfo[] dicInfoList = null; // 辞書情報の配列（出力）
    private DicInfo currentDict = null; // 辞書
    private DicItem[] itemList = null; // 辞書項目の配列（出力）

    private void Form1_Load(object sender, EventArgs e)
    {
      #region ComboBox に辞書リストを設定する。

      // Web辞書検索サービスのインスタンスを作成する。
      this.netDicV10 = new NetDicV10();
      // 辞書のリストを取得する。
      dicInfoList = netDicV10.GetDicList("");
      foreach (DicInfo dicInfo in dicInfoList)
      {
        // comboBox1に項目を追加
        int index = this.comboBox1.Items.Add(dicInfo.FullName);
        if (index == 2) break;
      }
      this.comboBox1.SelectedIndex = 0;

      #endregion
    }

    // 辞書が変更になった。
    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
      this.currentDict = this.dicInfoList[this.comboBox1.SelectedIndex];
    }

    private void button1_Click(object sender, EventArgs e)
    {
      DicInfo d = this.currentDict; // 選択された辞書
      uint reqItemIndex = 0; // 取得する辞書項目の開始インデックス
      uint reqItemTitleCount = 10; // 取得する辞書項目の数
      uint reqItemContentCount = 10; // 内容も同時に取得する辞書項目の数
      uint itemCountTotal;  // 見つかった辞書項目数（出力）

      Cursor cursor = this.Cursor;
      this.Cursor = Cursors.WaitCursor; // Wait Cursor にする。
      Query[] qw = new Query[1]; // クエリ構造体
      qw[0] = new Query();
      qw[0].Words = this.tbSearchText.Text;
      qw[0].ScopeID = d.ScopeList[0].ID;
      //グローバル一意識別子(GUID)の作成
      Guid[] dicGuid = new Guid[1];
      dicGuid[0] = System.Guid.NewGuid();
      dicGuid[0] = d.DicID;
      ContentProfile cp = new ContentProfile();
      cp.CharsetOption = CharsetOption.UNICODE; // 使用文字セット指定
      cp.FormatType = "XHTML";
      cp.ResourceOption = ResourceOption.URI;

      itemCountTotal = netDicV10.SearchDicItem(
        "", // 認証チケット文字列
        dicGuid, // 辞書ID
        qw, // 検索語(クエリ構造体)
        cp, // ContentProfile構造体
        "", // ソート用(使用しない)
        reqItemIndex,  // 取得する辞書項目の開始インデックス
        reqItemTitleCount, // 取得する辞書項目の数
        reqItemContentCount, // 内容も同時に取得する辞書項目の数
        out itemList  // 辞書項目の配列（出力）
        );
      this.Cursor = cursor;   // Cursor を元に戻す。

      this.labelMessage.Text =
        itemCountTotal.ToString() +
          "件みつかりました。最大１０件まで表示します。";
      this.listBox1.Items.Clear();
      foreach (DicItem dicItem in itemList)
      {
        this.listBox1.Items.Add(dicItem.Title.InnerText);
      }
    }

    // 検索結果のリストをセレクトしたので、詳細を表示する。
    private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
      string dictext =
        this.itemList[this.listBox1.SelectedIndex].Body.InnerText;
      this.richTextBox1.Text = dictext;
    }
  }
}
</pre>
<h3>Form1.Designer.cs</h3>
<pre>
namespace netdictist
{
  partial class Form1
  {
    /// &lt;summary&gt;
    /// 必要なデザイナ変数です。
    /// &lt;/summary&gt;
    private System.ComponentModel.IContainer components = null;

    /// &lt;summary&gt;
    /// 使用中のリソースをすべてクリーンアップします。
    /// &lt;/summary&gt;
    /// &lt;param name=&quot;disposing&quot;&gt;マネージ リソースが破棄される場合 true、破棄されない場合は false です。&lt;/param&gt;
    protected override void Dispose(bool disposing)
    {
      if (disposing &amp;&amp; (components != null))
      {
        components.Dispose();
      }
      base.Dispose(disposing);
    }

    #region Windows フォーム デザイナで生成されたコード

    /// &lt;summary&gt;
    /// デザイナ サポートに必要なメソッドです。このメソッドの内容を
    /// コード エディタで変更しないでください。
    /// &lt;/summary&gt;
    private void InitializeComponent()
    {
      this.comboBox1 = new System.Windows.Forms.ComboBox();
      this.tbSearchText = new System.Windows.Forms.TextBox();
      this.button1 = new System.Windows.Forms.Button();
      this.labelMessage = new System.Windows.Forms.Label();
      this.listBox1 = new System.Windows.Forms.ListBox();
      this.richTextBox1 = new System.Windows.Forms.RichTextBox();
      this.SuspendLayout();
      //
      // comboBox1
      //
      this.comboBox1.FormattingEnabled = true;
      this.comboBox1.Location = new System.Drawing.Point(14, 12);
      this.comboBox1.Name = &quot;comboBox1&quot;;
      this.comboBox1.Size = new System.Drawing.Size(204, 20);
      this.comboBox1.TabIndex = 0;
      this.comboBox1.SelectedIndexChanged += new System.EventHandler(this.comboBox1_SelectedIndexChanged);
      //
      // tbSearchText
      //
      this.tbSearchText.Location = new System.Drawing.Point(14, 51);
      this.tbSearchText.Name = &quot;tbSearchText&quot;;
      this.tbSearchText.Size = new System.Drawing.Size(204, 19);
      this.tbSearchText.TabIndex = 1;
      //
      // button1
      //
      this.button1.Location = new System.Drawing.Point(14, 87);
      this.button1.Name = &quot;button1&quot;;
      this.button1.Size = new System.Drawing.Size(204, 31);
      this.button1.TabIndex = 2;
      this.button1.Text = &quot;検索&quot;;
      this.button1.UseVisualStyleBackColor = true;
      this.button1.Click += new System.EventHandler(this.button1_Click);
      //
      // labelMessage
      //
      this.labelMessage.AutoSize = true;
      this.labelMessage.Location = new System.Drawing.Point(12, 136);
      this.labelMessage.Name = &quot;labelMessage&quot;;
      this.labelMessage.Size = new System.Drawing.Size(136, 12);
      this.labelMessage.TabIndex = 3;
      this.labelMessage.Text = &quot;最大１０件まで表示します。&quot;;
      //
      // listBox1
      //
      this.listBox1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
            | System.Windows.Forms.AnchorStyles.Left)));
      this.listBox1.FormattingEnabled = true;
      this.listBox1.ItemHeight = 12;
      this.listBox1.Location = new System.Drawing.Point(235, 12);
      this.listBox1.Name = &quot;listBox1&quot;;
      this.listBox1.Size = new System.Drawing.Size(160, 136);
      this.listBox1.TabIndex = 4;
      this.listBox1.SelectedIndexChanged += new System.EventHandler(this.listBox1_SelectedIndexChanged);
      //
      // richTextBox1
      //
      this.richTextBox1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
            | System.Windows.Forms.AnchorStyles.Left)
            | System.Windows.Forms.AnchorStyles.Right)));
      this.richTextBox1.Location = new System.Drawing.Point(14, 169);
      this.richTextBox1.Name = &quot;richTextBox1&quot;;
      this.richTextBox1.Size = new System.Drawing.Size(381, 241);
      this.richTextBox1.TabIndex = 5;
      this.richTextBox1.Text = &quot;&quot;;
      //
      // Form1
      //
      this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
      this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
      this.ClientSize = new System.Drawing.Size(407, 422);
      this.Controls.Add(this.richTextBox1);
      this.Controls.Add(this.listBox1);
      this.Controls.Add(this.labelMessage);
      this.Controls.Add(this.button1);
      this.Controls.Add(this.tbSearchText);
      this.Controls.Add(this.comboBox1);
      this.Name = &quot;Form1&quot;;
      this.Text = &quot;Form1&quot;;
      this.Load += new System.EventHandler(this.Form1_Load);
      this.ResumeLayout(false);
      this.PerformLayout();

    }

    #endregion

    private System.Windows.Forms.ComboBox comboBox1;
    private System.Windows.Forms.TextBox tbSearchText;
    private System.Windows.Forms.Button button1;
    private System.Windows.Forms.Label labelMessage;
    private System.Windows.Forms.ListBox listBox1;
    private System.Windows.Forms.RichTextBox richTextBox1;
  }
}
</pre>
<p>とりあえずこれで一応の動作はしますが、XMLの扱いがずさんなので、これから学んでいく必要があります。<br />
ともあれ、コーディング中感じたのは、Visual Studioのコード補正と宣言元へのジャンプ機能の強力さ。統合開発環境も使いこなしていきたいです。</p>
<h4>関連すると思われる記事：</h4>
<ul class="similar-posts">
<li><a href="http://www.yukun.info/blog/2008/02/csharp-timer.html" rel="bookmark" title="2008年2月24日">C#でキッチンタイマーを作ろう</a></li>
<li><a href="http://www.yukun.info/blog/2008/11/actionscript-flash-load-server-image-file.html" rel="bookmark" title="2008年11月21日">ActionScript: 画像ファイルをダウンロードして表示 &#8211; Loaderクラス</a></li>
<li><a href="http://www.yukun.info/blog/2008/06/python-dict2.html" rel="bookmark" title="2008年6月4日">Python: 辞書の全てのキーと値をたどる &#8211; items(), keys(), values()メソッド</a></li>
<li><a href="http://www.yukun.info/blog/2009/03/java-observe-event-model-view.html" rel="bookmark" title="2009年3月17日">Java: イベント駆動によるModelとViewの分離 &#8211; Observer パターン</a></li>
<li><a href="http://www.yukun.info/blog/2008/12/air-actionscript-sqlite-statement.html" rel="bookmark" title="2008年12月22日">AIR: SQLiteでデータの挿入と検索 &#8211; SQLConnection、SQLStatementクラス</a></li>
</ul>
<p><!-- Similar Posts took 9.267 ms --></p>
<p><a href="http://www.yukun.info/blog/2008/01/csharp-web-service-client.html">イースト辞書Webサービスを利用したC#(with .Net)クライアントプログラミング</a> is a post from: <a href="http://www.yukun.info">Yukun&#039;s Blog</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.yukun.info/blog/2008/01/csharp-web-service-client.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>チャットログから本文を抽出</title>
		<link>http://www.yukun.info/blog/2007/04/chatlog.html</link>
		<comments>http://www.yukun.info/blog/2007/04/chatlog.html#comments</comments>
		<pubDate>Tue, 10 Apr 2007 10:54:00 +0000</pubDate>
		<dc:creator>yukun</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Chatterbot]]></category>
		<category><![CDATA[RegExp]]></category>
		<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">http://codeai.net/?p=49</guid>
		<description><![CDATA[先日、メッセンジャーのチャットで会話するボットを作りました。 そのボットに「学習」させるネタに、会話文であるWindows Live Messengerのチャットログを用いることにしました。しかし、ログはXML形式なので &#8230; <a href="http://www.yukun.info/blog/2007/04/chatlog.html">Continue reading <span class="meta-nav">&#8594;</span></a><p><a href="http://www.yukun.info/blog/2007/04/chatlog.html">チャットログから本文を抽出</a> is a post from: <a href="http://www.yukun.info">Yukun&#039;s Blog</a></p>
]]></description>
			<content:encoded><![CDATA[<p>先日、メッセンジャーのチャットで会話するボットを作りました。 そのボットに「学習」させるネタに、会話文であるWindows Live Messengerのチャットログを用いることにしました。しかし、ログはXML形式なので、本文以外の余計なタグがはいっています。<br />
まあ、エディタで不用な部分を置換していっても良いのですが&#8230;</p>
<h4>抽出処理をRubyで自動化</h4>
<blockquote><p>仮に1時間かかる単調作業をするとしたら、私は50分かけても、その作業をこなすスクリプトを書く。</p></blockquote>
<p>と言う言葉を思いだしたので、早速、Windows Live Messenger(前MSN)のチャットログから本文のみを抽出するコードを書いてみました。</p>
<pre>
#! ruby -Ks
require ‘kconv’
MSNRegexp =/(.*?)&lt;.Text&gt;/xm
data = ""
filename = ARGV[0]
begin
  open(filename) do |f|
    f.each do |line| # ファイルから1行ずつ読む
    line = Kconv.kconv(line, Kconv::SJIS) # 文字コードを変換
    line.chomp!
    next if line.empty?
    data.concat(line)
  end
end
rescue => e
  puts(e.message)
end
open(’msnLog.txt’, ‘w’) do |f| # 出力ファイル
data.scan(MSNRegexp){|tdata|
  if tdata
    f.puts(tdata)
  end
}
end
</pre>
<p>上記のコードをプロンプトで抽出したいファイル名を引数に与えて 実行すると、msnLog.txtというファイルに本文が抽出されます。発言者なども抽出したい場合は、正規表現の部分を修正すれば良いですね。</p>
<p>では、今し方学習したボットとの会話はこんなです。 「<a href="http://www.yukun.info/blog/2007/04/chatbot-log.html" title="人工無脳とのチャットログ">とあるボットのチャットログ</a>」 やってる本人は、結構楽しんでましたね＾＾；と言うか合せてました。</p>
<h4>今後の改良点</h4>
<p>当面の課題は、辞書のメンテツールの作成及びデータベース化、人格形成、文脈・話者理解と情報収集・加工機能を利用環境に合せたエージェント化とか(ぅぉぃ)。</p>
<h4>関連すると思われる記事：</h4>
<ul class="similar-posts">
<li><a href="http://www.yukun.info/blog/2007/04/chatbot-log.html" rel="bookmark" title="2007年4月10日">とあるボットのチャットログ</a></li>
<li><a href="http://www.yukun.info/blog/2008/02/ruby-excite-translation.html" rel="bookmark" title="2008年2月8日">POSTメソッドを用いてExcite翻訳を行うRubyコード</a></li>
<li><a href="http://www.yukun.info/blog/2008/02/extract-web-tag.html" rel="bookmark" title="2008年2月10日">Webページから指定したタグの要素を抜き出すRuby関数</a></li>
<li><a href="http://www.yukun.info/blog/2008/05/java-excite-translation.html" rel="bookmark" title="2008年5月16日">JavaプログラムからExcite翻訳を利用</a></li>
<li><a href="http://www.yukun.info/blog/2008/10/actionscript-xml-element-attribute.html" rel="bookmark" title="2008年10月7日">ActionScript: XMLの子要素の読み込み &#8211; XMLオブジェクトと「.. 」「[]」演算子</a></li>
</ul>
<p><!-- Similar Posts took 10.114 ms --></p>
<p><a href="http://www.yukun.info/blog/2007/04/chatlog.html">チャットログから本文を抽出</a> is a post from: <a href="http://www.yukun.info">Yukun&#039;s Blog</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.yukun.info/blog/2007/04/chatlog.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

