���̂���XML: Ruby��XML�f�[�^������ (8) XML�t�@�C���𐶐��E�o�͂���
sample7.rb

2007�N11��18��(��)�X�V


���V���ɐ�������XML�f�[�^��XML�t�@�C���Ƃ��ďo��(sample7.rb)

������: �O��͓���XML�f�[�^�����ɂ��āAXML�t�@�C���ɏo�͂��Ă݂��ˁB����́A�܂������V������������XML�f�[�^���t�@�C���o�͂��Ă݂�ˁB

�����: ������ˁB

������: �S�z�Ȃ���B�܂��́A�v�f���ЂƂ‚����̊ȒP��XML�f�[�^�𐶐����āAXML�t�@�C���Ƃ��ďo�͂���ȒP�ȗ������Ă݂�ˁB

�����: �͂��B


����������XML�f�[�^

������: ��������XML�f�[�^��XML�t�@�C���̓��e�͎��̂悤�Ȃ��̂���B

��������XML�f�[�^

�����: sample7�Ƃ������O�̗v�f���ЂƂ‚ŁA�e�L�X�g���usample7: �T���v���f�[�^�ł��B�v�ƂȂ��Ă���̂ˁB

������: �������ˁB�����REXML�łǂ��������Ă䂭����}�Ő�������ˁB

XML�f�[�^�̐����菇

�����: �ŏ���Document�𐶐����Ă���A���ɗv�f��e�L�X�g�m�[�h��lj����Ă䂭�̂ˁB

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

�����: �͂��B


��Ruby�T���v���v���O����(7) sample7.rb�̃\�[�X�R�[�h

�sRuby�T���v���v���O����(7)�@sample7.rb
01
02
03
04
05
06
07
08
09
10
11

12
13
14
15
16
17

require "rexml/document"

out_file_name = "sample7_out.xml"

out_doc = REXML::Document.new()
out_doc.add(REXML::XMLDecl.new(version="1.0", encoding="SHIFT_JIS"))

root_element = REXML::Element.new("sample7")
out_doc.add_element(root_element)

root_element.add_text("sample7: �T���v���f�[�^�ł��B")


File.open(out_file_name,"w") do |outfile|

    out_doc.write(outfile, 0)

end


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


�������̊T�v

�����: �����`�ƁB�B�B

������: �T�s�ڂ���P�P�s�ڂŁAout_doc��XML�f�[�^�𐶐����Ă���񂾂��ǁA�ȒP�Ɍ��Ă䂱���ˁB

�����: �͂��B

  • �T�s��: out_doc = REXML::Document.new()
            Document�𐶐�
  • �U�s��: out_doc.add(REXML::XMLDecl.new(version="1.0", encoding="SHIFT_JIS"))
            Document��XML�錾��lj�
  • �W�s��: root_element = REXML::Element.new("sample7")
            sample7�v�f�𐶐�
  • �X�s��: root_element = REXML::Element.new("sample7")
            Document��sample7�v�f��lj�
  • �P�P�s��: root_element.add_text("sample7: �T���v���f�[�^�ł��B")
            Document��sample7�v�f�Ƀe�L�X�g��lj�

�����: �}�ƌ���ׂȂ���݂�ƕ������B

������: �P�R�s�ڂ���P�V�s�ڂŁAout_doc���t�@�C���Ƃ��ďo�͂��Ă��邯�ǁA����͑O��Ɠ���������킩���ˁB

�����: �͂��B


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

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

  • ruby sample7.rb

�Ɠ��͂��Ă݂āB

�����: �͂��B

�����: "sample7_out.xml"�Ƃ����t�@�C�����ł�����B

������: �����Ǝ��s�ł����݂������ˁB���Ⴀ�A"sample7_out.xml"�t�@�C���̓��e�����Ă݂悤�B

�o�͂���XML�f�[�^�@sample7_out.xml

<?xml version='1.0' encoding='SHIFT_JIS'?>
<sample7>sample7: �T���v���f�[�^�ł��B</poem> </sample7>

�����: �m����XML�t�@�C�����ł��Ă����B

������: �����ȒP��REXML::Document�̗Ⴞ�������ǁA�g�����������킩�������ȁB

�����: �����A�����A�����ˁB

������: ���񂩂�́A�����ƕ��G�Ȃ��̂�����Ă݂悤�ˁB

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

������: ����A����͂����܂ŁB

�����: �́`���B

�@���@�����������lj����� sample8.rb�ł��B (^ ^;


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