���̂���XML: Ruby��XML�f�[�^������ (12) �v�f���\�[�g���ĕ\��
sample12.rb

2008�N02��10��(��)�X�V


���v�f���\�[�g���ĕ\��(sample12.rb)

������: ����́A���͂���XML�f�[�^�̗v�f���\�[�g���ĕ\�����Ă݂悤�B

�����: �����A�͂��B

������: ����XML�t�@�C���̗v�f���\�[�g���ďo��XML�t�@�C�����‚��邱�Ƃ��������񂾂��ǁA����͂��̑O�ɗv�f���\�[�g���ĕ\�����Č��āA�\�[�g�̎d���̗���w��ł������Ǝv���񂾁B

�����: �����Ȃ񂾁B


���ǂݍ���XML�t�@�C��

������: �ǂݍ���XML�t�@�C���͂‚��̂��̂��g���ˁB

  • �ǂݍ���XML�t�@�C���� = sample12_in.xml
�ǂݍ���XML�t�@�C���@sample12_in.xml

<?xml version="1.0" encoding="utf-8" ?>
<manyo>
<poem no="0028" poet="�����V�c">�t�߂��ĉė���炵�������̈ߊ�������V�̍���R</poem>
<poem no="0161" poet="�����V�c">�k�R�ɂ��Ȃт��_�̐‰_�̐�����s�����𗣂��</poem>
<poem no="0397" poet="�}�Y��">���R�̊�{�������[�߂Č��т��S�Y�ꂩ�˂‚�</poem>
<poem no="0408" poet="�唺�Ǝ�">�Ȃł��������̉Ԃɂ������Ȓ��Ȏ�Ɏ�莝���ė��Ђʓ��Ȃ���</poem>
<poem no="0599" poet="�}�Y��">�����̂��قɑ������l�̂ɖ����ʂׂ����Ђ킽�邩��</poem>
<poem no="0611" poet="�唺�Ǝ�">������ɖ��Ɉ��͂߂�Ǝv�ւ����������䂪�����Ԃ���������</poem>
<poem no="1353">�Ώ�z���̑���c���G�ł��Ƃ��ꂾ�ɉ��ւ���‚‹����</poem>
<poem no="1616" poet="�}�Y��">�����Ƃɉ䂪����h�̂Ȃł����̉Ԃɂ��N�͂��肱���ʂ���</poem>
<poem no="2540">�U�����̔���Z�ݐ‘��𔯂ɂ�����ޖ��������v��</poem>
<poem no="4516" poet="�唺�Ǝ�">�V�����N�̏��߂̏��t�̍����~���̂��₵���g��</poem>
</manyo>

�����: ���l���̐l�̉̂�����ł����ˁB���ƁA�r�ݐl��������Ȃ��̂������ˁB

������: �������ˁB����̃\�[�g�\���̗�ł́A

  • �̐l���ƁA�̔ԍ��Ƀ\�[�g���ĕ\��
  • �̐l�̖����͕̂\�����Ȃ�
�Ƃ������Ƃ�����Ă݂悤�ˁB

�����: �͂��B

������: ����A�����Ȃ肾���ǁA��̓I�ȃ\�[�X�R�[�h�̗�����Ă݂悤�B


��Ruby�T���v���v���O����(12) sample12.rb�̃\�[�X�R�[�h

�sRuby�T���v���v���O����(12)�@sample12.rb
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31

require "rexml/document"
require "kconv"

xml_file_name = "sample12_in.xml"

doc = nil
File.open(xml_file_name) {|xmlfile|
  doc = REXML::Document.new(xmlfile)
}

print("--- ", xml_file_name, " ---\n\n")
# Hash(�n�b�V��)�𐶐�
poems = Hash.new

sample = doc.root
sample.elements.each { |poem|
  no =poem.attributes.get_attribute("no").value

  attr_poet = poem.attributes.get_attribute("poet")
  if attr_poet != nil then
    poet = attr_poet.value.tosjis
    poems[poet + no] = poem
  end
}
# poem�v�f���\�[�g
sorted = poems.sort
sorted.each {|key, poem|
  attr_poet = poem.attributes.get_attribute("poet")
  attr_no = poem.attributes.get_attribute("no")
  print(attr_poet.value.tosjis," ", attr_no," ", poem.text.tosjis, "\n")
}


�������ł́A�����R�[�h��utf-8�Ƃ���sample12.rb���쐬���Ă��܂��B

�����: ������ˁB(^ ^;

������: ����̃T���v���ł́AHash(�n�b�V��)���g���Ă����B

�����: Hash(�n�b�V��)���ĂȂ�?

������: ������g���ƊȒP�ɃL�[�Ń\�[�g�ł���񂾂�B����̗�ł�

  • �L�[ = �̐l + �̔ԍ�
  • �l = poem�v�f
�Ƃ��Ă��邩��ˁB

�����: �����Ȃ񂾁B


��Hash(�n�b�V��)�ɂ‚��ẮARuby�̃T�C�g�⏑�Ђ��Q�Ƃ��Ă��������ˁB

������: �ȒP�Ƀ\�[�X�R�[�h�̐��������Ă����ˁB�܂��́A�P�R�s�ڂŁAHash�𐶐����Ă���ˁB�����ł́A�܂��AHash�̒��g�͋�(����)������ˁB

�����: �͂��B

������: �P�U�`�Q�Q�s�ڂŁA�ǂݍ���XML�t�@�C����poem�v�f������Hash(�n�b�V��)�ɒlj����Ă���ˁB�ŁA�Q�Q�s�ڂŁApoem�v�f��Hash(�n�b�V��)�ɒlj����Ă���񂾂�B
    poems[poet + no] = poem

�����: �����Ȃ񂾁B�L�[���Apoet + no = poem���ĂƂ��Ȃ̂�����B�B

������: ���������B�ŁA�Q�U�s�ڂŁA�L�[�Ń\�[�g���Ă���񂾂�B
sorted = poems.sort

�����: ����A���ꂾ���ł�����?

������: ���������B�ӊO�ɊȒP�ł���B

�����: �����ˁBHash�ɒlj�����̂��ʓ|�����ǁB�B�B

������: ���������B�ŁA�Q�V�`�R�P�s�ڂŁA�\�[�g���ꂽHash���珇�ɃL�[�Ɨv�f(poem)�����o���ĕ\�����Ă����B

�����: "�Ȃ�Ƃ�.each" ���đO�ɂ���������ˁB���̂Ƃ���Hash����Ȃ��������ǁB


���T���v���v���O����sample12.rb�̎��s���ʂ̊m�F

������: ����A����sample12.rb�����s�������ʂ��݂Ă݂悤�B�R�}���h�v�����v�g�E�B���h�E��

  • ruby sample12.rb

�Ɠ��͂��Ă݂āB

�����: �͂��B

sample12.rb�̎��s���ʊm�F

�����: �̐l�̖��O�Ɖ̔ԍ��̌�ɁA�̂̓��e���\������Ă����B����ɁA�̐l���A�̔ԍ����ɁB

������: �����Ǝ��s�ł����݂������ˁB

�����: ��������āA����ƕ��������C�������B

������: ����̃T���v���𓥂܂��āA����́A����XML�t�@�C�������ɁA�̐l���ƁA�̔ԍ����Ƀ\�[�g���ďo��XML�t�@�C��������Ă݂悤�ˁB

�����: �����A�͂��B�����������ƂˁB

�@���@������v�f���\�[�g���ďo�� sample13.rb�ł��B (^ ^;


[���̂���XML �̃C���f�b�N�X]��